Question

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:

  1. numeric (empty and string are not valid). When V1 or V2 are not valid a message windows appears.
  2. when variable 2 is more than 80 a message appears and ask a confirm.

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()
Was it helpful?

Solution

  1. Remove while loop.
  2. Since there is no loop now, Remove break.
  3. Indent accordingly.

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top