返回Tkinter文本输入的值,关闭GUI [英] Return values of Tkinter text entry, close GUI

查看:105
本文介绍了返回Tkinter文本输入的值,关闭GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下无法正常工作的GUI代码。我希望它执行以下操作:

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


  1. 对于Submit函数,我希望它检查Val1或Val2是否为空,然后警告用户并暂停该功能,以允许用户输入一个值,然后执行其余功能。这包括关闭GUI(除了手动关闭外,我不知道该怎么做)。

  2. 我还希望GUI从类中返回Val1和Val2。 。代码的最后一行是 print Total,这是我为返回值指定的名称。

  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



推荐答案

Hrrm ...您确定不想只让Val1和Val2作为Values类的属性,并让Submit按钮设置值吗?

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?

然后您可以随时使用self.Val1和self.Val2检查/返回/使用它们?另外,您也可以使用self.destroy()或self.quit()销毁窗口(查找每种方法并确定哪种方法对您有用)。

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通常,按钮回调不以您描述的方式用于返回值。通常,他们将运行一些对类的属性进行处理或修改的函数。

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.

另外,请记住,退出mainloop后可以访问这些属性,这似乎是您要使用它们进行的操作:

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:

编辑:下面是代码的稍微简化的版本。我删除了消息框的内容,制作了类的值和fields属性,并在您的提交按钮中添加了quit()方法。

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

这篇关于返回Tkinter文本输入的值,关闭GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆