我有以下GUI代码,我无法开始工作。我希望它执行以下操作:
我还希望GUI将Val1和Val2退出课堂。代码的最后一行是“print Total”,这是我给返回值的名称。
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
答案 0 :(得分:2)
Hrrm ...你确定你不想让Val1和Val2成为Values类的属性,并让submit按钮设置值吗?
然后你可以随时检查/返回/使用它们self.Val1和self.Val2?你也可以使用self.destroy()或self.quit()来破坏窗口(查看每个方法并确定哪个对你有用)。
通常,按钮回调不会用于以您描述的方式返回值。通常,它们将运行一些函数来处理或修改类的属性。
另外,请记住,退出mainloop后可以访问这些属性,这似乎是你想要用它们做的事情:
编辑:下面是您的代码的略微简化版本。我删除了消息框的内容,创建了类的值和fields属性,并在提交按钮中添加了一个quit()方法。
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