简体   繁体   English

Python Tkinter按钮/条目组合

[英]Python Tkinter Button/Entry combination

I've been experimenting with Tkinter, and I have a set-up where I have an entry box and then below it two buttons (Close and OK). 我一直在尝试Tkinter,我有一个设置,其中有一个输入框,然后在它下面有两个按钮(“关闭”和“确定”)。 When close is clicked, the frame is destroyed. 单击关闭时,框架被破坏。 I want it to return whatever is in the entry box at the time and then destroy the frame. 我希望它返回当时输入框中的内容,然后销毁框架。 I am just at a loss for how to do this. 我只是茫然如何做到这一点。

This is a portion of what I have (where f is my frame): 这是我所拥有的一部分(其中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")

Also, please note that I want to RETURN the string to the calling function, not just immediately print it. 另外,请注意,我想将字符串返回给调用函数,而不仅仅是立即打印它。

I want: 我想要:

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

What you ask is, for all intents and purposes, impossible. 就所有意图和目的而言,您要问的是不可能的。 The function DoThis will return before the GUI even displays on the screen. DoThis函数将在GUI甚至在屏幕上显示之前返回。

That being said, such a thing is possible, though highly unusual. 话虽如此,尽管非常不寻常,但这样的事情是可能的。 It's a bit like asking how you can haul a bale of hay across a muddy field in a Ferrari. 这有点像问如何在法拉利的泥泞的田野上拖一捆干草。

If you just plan on popping up a window once, you can get by with something like the following: 如果您仅打算一次弹出一个窗口,则可以通过以下方式获得帮助:

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

If you are going to open the window more than once you're probably not going to have much luck because, again, this just isn't how Tkinter is designed to be used. 如果您不止一次要打开窗户,那么可能不会很幸运,因为同样,这也不是Tkinter设计使用的方式。

at the moment you're assigning ButtonA, Cancel, command to root.destroy. 目前,您正在将ButtonA(取消)命令分配给root.destroy。 Instead of directly calling the destroy function, create a separate function that reads the value then calls destroy. 创建直接读取值然后调用destroy的单独函数,而不是直接调用destroy函数。

I usually wrap this in a Frame class to make it a little easier: 我通常将其包装在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()

Basically, you want your callback function from the button to be a bit more complicated. 基本上,您希望按钮的回调函数更加复杂。 Rather than simply calling the destroy method, you'll want to call your own function. 您将要调用自己的函数,而不是简单地调用destroy方法。 Use the get method on the Entry object to retrieve the contents. 在Entry对象上使用get方法检索内容。

Hopefully this is a complete enough example to get you going: 希望这是一个足够完整的示例,可以帮助您:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM