while循环在Tkinter中用Python插入数据

时间:2014-04-28 19:49:32

标签: python while-loop coding-style tkinter

我是Tkinter的新手,我在While-loop导入数据def save.时遇到问题 这是一个例子。我有两个数字变量Variable 1和Variable 2,它们需要是:

  1. numeric(空和字符串无效)。当V1或V2不是 有效的消息窗口出现。
  2. 当变量2大于80时,会出现一条消息并询问a 确认。
  3. 我有问题要了解这是否是最好的方法以及如何定位休息

    from Tkinter import *
    import tkMessageBox
    
    
    class MainWindow(Frame):
        def __init__(self):
            Frame.__init__(self)
            self.master.title("input")
            self.master.minsize(250, 150)
            self.grid(sticky=E+W+N+S)
    
            self.res=StringVar()
            self.res.set("---")
    
            top=self.winfo_toplevel()
            top.rowconfigure(0, weight=1)
            top.columnconfigure(0, weight=1)
    
            for i in range(4):self.rowconfigure(i, weight=1)
            self.columnconfigure(1, weight=1)
    
            self.label0 = Label(self, text="value 1   ")
            self.label0.grid(row=0, column=0, padx=2)
    
            self.entry0 = Entry(self)
            self.entry0.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)
    
            self.label1 = Label(self, text="value 2   ")
            self.label1.grid(row=1, column=0, padx=2)
    
            self.entry1 = Entry(self)
            self.entry1.grid(row=1, column=1, pady=2, padx=2, sticky=E+W+N+S)
    
            self.button1 = Button(self, text="Save", command=self.save)
            self.button1.grid(row=2, column=0, columnspan=2, pady=2, padx=2, sticky=E+W+N+S)
    
            self.result = Label(self, textvariable=self.res)
            self.result.grid(row=3, column=0, columnspan=2, pady=2, padx=2, sticky=E+W+N+S)
    
        def save(self):
            while True:
                v1 = self.entry0.get()
                if not v1:
                    tkMessageBox.showwarning(title="Warning message box", message="The Value 1 is empty")
                else:
                    try:
                        v1 = float(v1)
                        break
                    except:
                        tkMessageBox.showwarning(title="Warning message box", message="The value 1 is not a numeric value")
            while True:
                v2 = self.entry1.get()
                if not v2:
                    tkMessageBox.showwarning(title="Warning message box", message="The Value 2 is empty")
                else:
                    try:
                        v2 = float(v2)
                        if v2 >= 80:
                            if tkMessageBox.askyesno(title="Value 2",message="The value 2 is %s. Are you sure?" % v2) == 1:
                                v2 = v2
                                break
                        else:
                            break
                    except:
                        tkMessageBox.showwarning(title="Warning message box", message="The value 2 is not a numeric value")
            self.res.set(str(v1 * v2))
    
    if __name__=="__main__":
       d=MainWindow()
       d.mainloop()
    

2 个答案:

答案 0 :(得分:1)

  1. 删除while循环。
  2. 由于现在没有循环,请删除break
  3. 相应地缩进。
  4. 您的最终代码可能如下所示:

    from Tkinter import *
    import tkMessageBox
    
    
    class MainWindow(Frame):
        def __init__(self):
            Frame.__init__(self)
            self.master.title("input")
            self.master.minsize(250, 150)
            self.grid(sticky=E+W+N+S)
    
            self.res=StringVar()
            self.res.set("---")
    
            top=self.winfo_toplevel()
            top.rowconfigure(0, weight=1)
            top.columnconfigure(0, weight=1)
    
            for i in range(4):self.rowconfigure(i, weight=1)
            self.columnconfigure(1, weight=1)
    
            self.label0 = Label(self, text="value 1   ")
            self.label0.grid(row=0, column=0, padx=2)
    
            self.entry0 = Entry(self)
            self.entry0.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)
    
            self.label1 = Label(self, text="value 2   ")
            self.label1.grid(row=1, column=0, padx=2)
    
            self.entry1 = Entry(self)
            self.entry1.grid(row=1, column=1, pady=2, padx=2, sticky=E+W+N+S)
    
            self.button1 = Button(self, text="Save", command=self.save)
            self.button1.grid(row=2, column=0, columnspan=2, pady=2, padx=2, sticky=E+W+N+S)
    
            self.result = Label(self, textvariable=self.res)
            self.result.grid(row=3, column=0, columnspan=2, pady=2, padx=2, sticky=E+W+N+S)
    
        def save(self):
                v1 = self.entry0.get()
                if not v1:
                    tkMessageBox.showwarning(title="Warning message box", message="The Value 1 is empty")
                else:
                    try:
                        v1 = float(v1)
                    except:
                        tkMessageBox.showwarning(title="Warning message box", message="The value 1 is not a numeric value")
    
                v2 = self.entry1.get()
                if not v2:
                    tkMessageBox.showwarning(title="Warning message box", message="The Value 2 is empty")
                else:
                    try:
                        v2 = float(v2)
                        if v2 >= 80:
                            if tkMessageBox.askyesno(title="Value 2",message="The value 2 is %s. Are you sure?" % v2) == 1:
                                v2 = v2
                        else:
                            pass
                    except:
                        tkMessageBox.showwarning(title="Warning message box", message="The value 2 is not a numeric value")
                self.res.set(str(v1 * v2))
    
    if __name__=="__main__":
        d=MainWindow()
        d.mainloop()
    

答案 1 :(得分:0)

我不会在save()函数中使用While循环。 mainloop()使表单保持打开状态。摆脱那些While循环,它们不是必需的,当你单击没有值的按钮时它会在无限循环中运行。此外,您可以使用IntVar()而不是StringVar()来避免转换浮动然后转换为字符串。