I am new of Tkinter and i have problem with the While-loop
to import data in def save.
This is an example. I have two numeric variable Variable 1 and Variable 2, they need to be:
I have problem to understand if this is the best way and how to positioning the break
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()
while
loop. break
. Your final code may look something like this:
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()
I wouldn't use a While loop inside your save() function. mainloop() keeps the form open. Get rid of those While loops, they are not necessary, it runs in an infinite loop when you click the button with no values. Also you can avoid the conversions to float and then to string by using IntVar() instead of StringVar().
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.