Python 3中的失败记录程序

时间:2018-02-28 19:16:01

标签: python-3.x tkinter

我编写了一个脚本,可以保存客户的设备信息,如下所示。是否可以使用Python Tkinter将此代码转换为GUI。我该怎么办?

    print("FAILURE LOGGING PROGRAM")

filename = input ("Please enter the file name: ")+(".txt");
with open (filename, "w") as f:
    f.write ("Date: " + input ("Date: "));
    f.write ("\n\Record No: " + input ("Record No: "));
    f.write ("\n\nCustomer Name: " + input ("Customer Name: "));
    f.write ("\n\nCustomer Phone Number: " + input ("Customer Phone Number: "));
    f.write ("\n\nCustomer Adress: " + input ("Customer Adress: "));
    f.write ("\n\nDevice Type: " + input ("Device Type: "));
    f.write ("\n\nTrademark : " + input ("Trademark: "));
    f.write ("\n\nImei Number: " + input ("Imei Number: "));
    f.write ("\n\nFailure Explanation: " + input ("Failure Explanation: "));
    f.write ("\n\nRepair Price: " + input ("Repair Price: "));
    f.write ("\n\nExpected Repair Time: " + input ("Expected Repair Time: "));
    f.write ("\n\nAnnotation: " + input ("Annotation: "));

2 个答案:

答案 0 :(得分:1)

是的,这是可能的。您要使用的主要内容是标签,条目和按钮。标签显示文本,条目获得文本输入,按钮是调用功能的按钮。

这是一个基本的例子:

#You Shouldn't Always Import Tkinter Like This, But In This Case, You Can
from tkinter import *
class GUI:
    def __init__(master):
        """
        Specify The Master or Root Frame First, Then Any Other Parameter
        Labels: Display Text
        Entries: Get One-Line Text Inputs
        Buttons: A Button That Runs A Command
        Grid Places The Widget Wherever you Want (There Are Also Other Layouts Like Pack and Place)
        """
        Label(master,text="Label:").grid(row=0,column=0)
        self.entry = Entry(master)
        """
        Places Entry On Same Row But Not Column Of Label
        Notice How Grid Is Not Called On Constructor But On Variable Later (For More Info: https://stackoverflow.com/a/1101765/8935887)
        """
        self.ent.grid(row=0,column=1)
        Button(master,text="Click!",command=self.func).grid(row=1,column=1)
    def func(self):
        text = self.ent.get()
        print('Our Text Is:',text)
#Basically Checks If Program is not being Imported
if __name__ == '__main__':
    root = Tk()
    GUI(root)
    #Starts Tkinter Event Loop
    root.mainloop()

另一个注意事项:您通常应该在类中创建GUI,因为以后会更容易。

答案 1 :(得分:0)

是的,有可能将它转换成百万种方式。阅读tutorial