【发布时间】:2013-05-01 15:48:29
【问题描述】:
我将如何在 GUI 中包装此文本?一般来说,我对 python 和编程相当陌生,而且我把这个转换器写成第一个项目。我现在想将项目包装在 GUI 中,但是当我开始阅读有关 tkinter 的内容时,看起来好像我必须重写我的整个代码。是否可以在不完全重写的情况下围绕它包装一个 GUI?
__Version__ = 0.1
import time
import os
from tkinter import Tk
line_break = "==========_Number_in_Inches_=========="
MM_break = "==========_Enter_MM_To_Convert_=========="
sys_Error = "ValueError, Could not recognize input as a number"
Inches = float(25.4)
def main():
while True:
os.system('CLS')
print("Welcome to the converter")
time.sleep(0.5)
print("What would you like to do?")
time.sleep(0.5)
print("Enter Convert MM or Convert Surface")
function = (input())
time.sleep(0.5)
if function in('Convert','convert'):
con()
elif function in('Surface', 'surface', 'RA', 'ra', 'Ra'):
surf()
elif function in('end', 'kill', 'quit', 'End', 'Kill', 'Quit'):
print("Goodbye")
time.sleep(1.00)
os.system('CLS')
time.sleep(1.00)
break
elif function in('help', 'Help'):
help()
else:
print(sys_Error)
time.sleep(1.0)
def con():
while True:
print("Return = Main Menu, Surface = RA Conversion")
print(MM_break)
number = (input())
if number in('Return', 'return'):
break
elif number in('Surface', 'surface', 'Ra', 'RA', 'ra'):
surf()
elif number in('help', 'Help'):
help()
elif number in('end', 'kill', 'quit', 'End', 'Kill', 'Quit'):
break
else:
try:
float(number)
except ValueError:
print(sys_Error)
break
else:
float_number = float(number)
Convert = float_number/Inches
Results_3 = ("%.3f" % Convert)
Results_4 = ("%.4f" % Convert)
print(line_break)
print(" ")
print('\t', Results_3)
print('\t', Results_4)
print(line_break)
print(" ")
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(Results_3)
def surf():
while True:
print("Please enter Surface Finish")
print("or type return to return to main menu")
surface = (input())
if surface in('Return', 'return'):
break
elif surface in('Convert', 'convert'):
con()
elif surface in('help', 'Help'):
help()
elif surface in('end', 'kill', 'quit', 'End', 'Kill', 'Quit'):
break
else:
try:
float(surface)
except ValueError:
print(sys_Error)
time.sleep(1.0)
break
else:
float_surface = float(surface)
RA_convert = (float_surface/Inches) * 1000
Results = float(RA_convert)
RA_results = ("%.0f" % Results)
print(RA_results)
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(RA_results)
def help():
while True:
print('\t' "If you are having trouble")
print('\t' "Make sure you are prompting the program")
print('\t' "to enter etiher MM conversion mode")
print('\t' "or Surface Conversion mode")
print('\t' "by entering Convert or Surface")
print('\t' "when prompted at startup")
print('\t' "make sure that you are entering a number")
print('\t' "or a recognized command")
print('\t' "The list of commands are as follows")
print('\t' "help = Displays help message")
print('\t' "end = Ends the program")
print('\t' "(also try End, quit, Quit, kill, Kill)")
print('\t' "Convert = MM Conversion mode")
print('\t' "Surface or Ra = Surface conversion mode")
print('\t' "If you are still having problems contact")
print('\t' "Daniel Granado")
print('\t' "at Dan_granado@pennunited.com")
time.sleep(4.0)
print("Press enter to return to main menu")
reload = input()
break
main()
【问题讨论】:
-
你的缩进搞砸了——你的方法定义在
defs下没有正确缩进。 -
@BryanOakley 感谢您指出这一点。我刚刚从我的文本编辑器中复制了代码并粘贴了它。它的格式一定不正确。
标签: user-interface python-3.x tkinter