【问题标题】:Python Tkinter Button/Entry combinationPython Tkinter 按钮/条目组合
【发布时间】:2012-02-20 03:10:53
【问题描述】:

我一直在试验 Tkinter,我有一个设置,我有一个输入框,然后在它下面有两个按钮(关闭和确定)。单击关闭时,框架被销毁。我希望它返回当时输入框中的任何内容,然后销毁框架。我只是不知道如何做到这一点。

这是我所拥有的一部分(f 是我的框架):

class App:
    def DoThis(self):
        #Earlier code that's not related to the question
        v=StringVar()
        e=Entry(f,textvariable=v)
        buttonA=Button(f,text="Cancel",command=root.destroy)
        buttonB=Button(f,text="OK")

另外,请注意,我想将字符串返回给调用函数,而不是立即打印。

我想要:

print App().DoThis() #to print what was in the entry box
#at the time of OK being clicked

【问题讨论】:

  • "return" 在事件驱动程序的上下文中没有意义。您希望将 tne 值返回到哪里?
  • 我添加了更多代码,希望能消除您的困惑。

标签: python button tkinter tkinter-entry


【解决方案1】:

无论出于何种意图和目的,您所问的都是不可能的。函数DoThis 将在 GUI 甚至显示在屏幕上之前返回。

话虽如此,这样的事情是可能的,尽管非常不寻常。这有点像问你如何在法拉利的泥地上拖着一捆干草。

如果您只打算弹出一次窗口,则可以通过以下方式解决:

import Tkinter as tk

class MyApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.entry = tk.Entry(self)
        self.entry.pack()
        close_button = tk.Button(self, text="Close", command=self.close)
        close_button.pack()
        self.string = ""

    def close(self):
        global result
        self.string = self.entry.get()
        self.destroy()

    def mainloop(self):
        tk.Tk.mainloop(self)
        return self.string

print "enter a string in the GUI"
app = MyApp()
result = app.mainloop()
print "you entered:", result

如果您要多次打开窗口,您可能不会有太多的运气,因为这又不是 Tkinter 的设计初衷。

【讨论】:

  • 通过编辑你的一些代码,我设法让它直接返回值,而不是仅仅设置一个等于它的全局变量。无论哪种方式,您都提供了极大的帮助,非常感谢您!
【解决方案2】:

此时您正在将 ButtonA、Cancel、命令分配给 root.destroy。不要直接调用destroy函数,而是创建一个单独的函数来读取值然后调用destroy。

我通常将其包装在 Frame 类中以使其更容易:

import Tkinter

class Frame(Tkinter.Frame):

def __init__(self, root=None):
    self.root = root
    Tkinter.Frame.__init__(self, root)

    # Create widgets
    self.v = StringVar()
    self.e = Entry(self, textvariable=v)
    self.buttonA = Button(self, text="Cancel", command=cancel)
    self.buttonB = Button(self, text="OK")

def cancel(self):
    print self.v.get()  # handle value here
    self.root.destroy()

【讨论】:

  • 问题是我希望它返回值,如果我只是将您的print self.v.get() 更改为return self.v.get(),它将返回到__init__ 而不是主类。跨度>
【解决方案3】:

基本上,您希望按钮的回调函数更复杂一些。您需要调用自己的函数,而不是简单地调用 destroy 方法。使用 Entry 对象上的 get 方法来检索内容。

希望这是一个足够完整的示例,可以让您继续前进:

import Tkinter as tk

class App:
    def __init__(self, master):
        self.display_button_entry(master)

    def setup_window(self, master):
        self.f = tk.Frame(master, height=480, width=640, padx=10, pady=12)
        self.f.pack_propagate(0)

    def display_button_entry(self, master):
        self.setup_window(master)
        v = tk.StringVar()
        self.e = tk.Entry(self.f, textvariable=v)
        buttonA = tk.Button(self.f, text="Cancel", command=self.cancelbutton)
        buttonB = tk.Button(self.f, text="OK", command=self.okbutton)
        self.e.pack()
        buttonA.pack()
        buttonB.pack()
        self.f.pack()

    def cancelbutton(self):
        print self.e.get()
        self.f.destroy()

    def okbutton(self):
        print self.e.get()


def main():
    root = tk.Tk()
    root.title('ButtonEntryCombo')
    root.resizable(width=tk.NO, height=tk.NO)
    app = App(root)
    root.mainloop()

main()

【讨论】:

  • 问题是我希望它返回值,而不是打印它。如果我只是将代码中的print 语句更改为return,那么它只会将其返回给display_button_entry,而不是主类。
  • 我必须查看更多您的代码,但我猜您是在尝试做错事。您不应该希望按钮返回值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-23
  • 1970-01-01
  • 2020-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多