使用tkinter进行GUI

时间:2013-05-01 15:48:29

标签: user-interface python-3.x tkinter

我如何在GUI中包装此文本?我是python和编程的新手,我把这个转换器写成第一个项目。我现在想要将项目包装在GUI中,但是当我开始阅读有关tkinter的内容时,我看起来似乎必须重写整个代码。是否可以在不完全重写的情况下围绕此包装?

 __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()      

2 个答案:

答案 0 :(得分:0)

在某些方面,你可以,但这将证明是困难的。您可以使用按钮或菜单栏来执行命令,然后使用标签来执行文本。希望有所帮助!

答案 1 :(得分:0)

简短答案:

从技术上讲您可以,但是您必须更改很多代码。您必须使用标签,按钮等,并且由于您是初学者,因此应该通过从头开始构建应用来学习tkinter。