1
我該如何去包裝這個文本在GUI中?我對python和編程一般都很陌生,而且我寫了這個轉換器作爲第一個項目。我現在想要在GUI中包裝項目,但是當我開始閱讀tkinter時,看起來好像我將不得不重寫我的整個代碼。是否有可能在不用完全重寫它的情況下將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 [email protected]")
time.sleep(4.0)
print("Press enter to return to main menu")
reload = input()
break
main()
您的縮進被搞砸了 - 您的方法定義在「def」下面沒有正確縮進。 – 2013-05-01 16:27:25
@BryanOakley謝謝你指出。我只是從我的文本編輯器複製了代碼並將其粘貼。它不能格式化正確。 – 2013-05-01 16:38:00