Return values of Tkinter text entry, close GUI

2019-05-22 21:02发布

I have the following GUI code, that I cant't get to work. I want it do the following:

  1. For the submit function, I want it to check if the Val1 or Val2 are blank and then warn the user and pause the function to allow the user to enter a value and then carryout the rest of the function. This includes closing the GUI (which I don't know how to do, except closing it manually).
  2. I also want the GUI to return Val1 and Val2 out of the class. The last line of the code is "print Total", which is the name I have given to the returned values.

    import Tkinter
    import tkMessageBox
    class Values(Tkinter.Tk):
       def __init__(self,parent):
            Tkinter.Tk.__init__(self,parent)
            self.parent = parent
            self.initialize()
       def initialize(self):
            self.grid()
            stepOne = Tkinter.LabelFrame(self, text=" 1. Enter Values ")
            stepOne.grid(row=0, columnspan=7, sticky='W',padx=5, pady=5, ipadx=5, ipady=5)
            Val1Lbl = Tkinter.Label(stepOne,text="Value 1")
            Val1Lbl.grid(row=0, column=0, sticky='E', padx=5, pady=2)
            Val1Txt = Tkinter.Entry(stepOne)
            Val1Txt.grid(row=0, column=1, columnspan=3, pady=2, sticky='WE')
            Val2Lbl = Tkinter.Label(stepOne,text="Value 2")
            Val2Lbl.grid(row=1, column=0, sticky='E', padx=5, pady=2)
            Val2Txt = Tkinter.Entry(stepOne)
            Val2Txt.grid(row=1, column=1, columnspan=3, pady=2, sticky='WE')
        def submit():
           Val1=Val1Txt.get()
           if Val1 == '':
               Win2=Tkinter.Tk()
               Win2.withdraw()
               tkMessageBox.showinfo(message="Value 1 is empty")
              ##Stop submit from going any further.Allow user to enter a value and then          
              ##carryout.  
    
            Val2=Val2Txt.get()
            if Val2 == '':
                Win2=Tkinter.Tk()
                Win2.withdraw()
                tkMessageBox.showinfo(message="Value 2 is empty")
                ###Stop submit from going any further.Allow user to enter a value and then
                ##carryout
    
             ###Close GUI (Part of submit function)
             return Val1,Val2
    
         SubmitBtn = Tkinter.Button(stepOne, text="Submit",command=submit)
         SubmitBtn.grid(row=4, column=3, sticky='W', padx=5, pady=2)
    if__name__== "__main__":  
         app = Values(None)
         app.title('Values')
         app.mainloop()
    
    
    ###Do something with returned values
    Total = Values##Is this the correct way of getting the returned values?
    print Total
    

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-05-22 21:15

Hrrm... are you sure you don't want to just have Val1 and Val2 be attributes of the Values class, and have the submit button set the values?

Then you can check/return/use them whenever you want with self.Val1 and self.Val2? Also you can destroy the window with self.destroy() or self.quit() (look each of these methods up and determine which is useful to you).

In general, button callbacks are not used to return values in the way that you are describing. Typically they will run some function that does some processing or modifying of the class's attributes.

Also, remember, these attributes can be accessed after exiting the mainloop, which appears to be the sort of thing that you are wanting to do with them:

Edit: Below is a slightly simplified version of your code. I removed the message box stuff, made the values and the fields attributes of your class, and added a quit() method in your submit button.

import Tkinter


class Values(Tkinter.Tk):
    """docstring for Values"""
    def __init__(self, parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()
        stepOne = Tkinter.LabelFrame(self, text=" 1. Enter Values ")
        stepOne.grid(row=0, columnspan=7, sticky='W',padx=5, pady=5, ipadx=5, ipady=5)
        self.Val1Lbl = Tkinter.Label(stepOne,text="Value 1")
        self.Val1Lbl.grid(row=0, column=0, sticky='E', padx=5, pady=2)
        self.Val1Txt = Tkinter.Entry(stepOne)
        self.Val1Txt.grid(row=0, column=1, columnspan=3, pady=2, sticky='WE')
        self.Val2Lbl = Tkinter.Label(stepOne,text="Value 2")
        self.Val2Lbl.grid(row=1, column=0, sticky='E', padx=5, pady=2)
        self.Val2Txt = Tkinter.Entry(stepOne)
        self.Val2Txt.grid(row=1, column=1, columnspan=3, pady=2, sticky='WE')

        self.val1 = None
        self.val2 = None

        SubmitBtn = Tkinter.Button(stepOne, text="Submit",command=self.submit)
        SubmitBtn.grid(row=4, column=3, sticky='W', padx=5, pady=2)

    def submit(self):
        self.val1=self.Val1Txt.get()
        if self.val1=="":
            Win2=Tkinter.Tk()
            Win2.withdraw()

        self.val2=self.Val2Txt.get()
        if self.val2=="":
            Win2=Tkinter.Tk()
            Win2.withdraw()

        self.quit()


if __name__ == '__main__':
    app = Values(None)
    app.title('Values')
    app.mainloop() #this will run until it closes
    #Print the stuff you want.
    print app.val1,app.val2
查看更多
登录 后发表回答