gpt4 book ai didi

python - Tkinter 文本输入的返回值,关闭 GUI

转载 作者:行者123 更新时间:2023-11-28 17:46:48 25 4
gpt4 key购买 nike

我有以下 GUI 代码,但我无法开始工作。我希望它执行以下操作:

  1. 对于提交功能,我希望它检查 Val1 或 Val2 是否为空,然后警告用户并暂停该功能以允许用户输入一个值,然后执行该功能的其余部分。这包括关闭 GUI(我不知道该怎么做,除非手动关闭它)。
  2. 我还希望 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

最佳答案

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

然后你可以随时用 self.Val1 和 self.Val2 检查/返回/使用它们?您也可以使用 self.destroy() 或 self.quit() 销毁窗口(查看这些方法中的每一个并确定哪个对您有用)。

一般而言,按钮回调不用于以您描述的方式返回值。通常他们会运行一些函数来处理或修改类的属性。

此外,请记住,这些属性可以在退出主循环后访问,这似乎是您想要用它们做的事情:

编辑:下面是您的代码的一个稍微简化的版本。我删除了消息框的东西,制作了你的类的值和字段属性,并在你的提交按钮中添加了一个 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

关于python - Tkinter 文本输入的返回值,关闭 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17032089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com