在 Tkinter 中单击按钮时如何调用操作 [英] How to call an action when a button is clicked in Tkinter

查看:42
本文介绍了在 Tkinter 中单击按钮时如何调用操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试使用 Tkinter,并尝试在单击按钮时调用函数.这是我的代码的一部分.mt 指的是我通过将其附加到标签而使其动态化的标签,以便我可以更改标签的内容.我希望用户能够在输入框中输入一些内容,点击按钮,它会将标签更改为输入的内容.

I am experimenting with Tkinter for the first time, and am trying to call a function when a button is clicked. This is part of my code. mt is referring to a label that I have made dynamic by attaching it to a label so that I can change what the label says. I want the user to be able to type in something into an entry box, hit the button, and the it will change the label to what was typed.

    def new(self):
        mt.set("New")
        e1 = Entry(master)
        e1.pack()
    def new_ok(self):
        mt.set("OK")
        #the next part is what I need help with
        if (checks if button has been clicked) button has been clicked:
            mt.set("#what it says in the entry box#")

我该怎么做?我查看并阅读了教程,但没有一个明确说明如何检查按钮是否被按下并做出响应.

How should I do this? I have looked on tutorials and read them but none of them have clearly acknowledged how to check if a button has been pressed and to respond.

推荐答案

如果问题是:如何更新标签小部件?"
那么答案是使用小部件的 configure 方法.

If the question is: "How do you update a Label widget?"
then the answer is with the widget's configure method.

# Tkinter in Python 2.7 & tkinter in 3.2
import Tkinter as tk

class GUI(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        bF = tk.Frame(self, bd=8, relief='sunken')
        bF.pack(expand='true', fill='x')
        changeButton = tk.Button(bF, text='Change', bd=4, fg='white',
                                relief='groove', activebackground='green',
                                command=self.change_label)
        changeButton.pack()

        self.entryLabel = tk.Label(self, text='Hello')
        self.entryLabel.pack()

        self.mEntry = tk.Entry(self, bd=4, relief='sunken')
        self.mEntry.pack()

    def change_label(self):
        data = self.mEntry.get()
        self.entryLabel.configure(text=data)


gui = GUI()
gui.mainloop()

你会想让你的 GUI 变成一个像这个例子中的类;
这样你就可以使用自我.前缀来引用用另一种方法制作的小部件.

You will want to make your GUI a class like in this example;
that way you can use the self. prefix to refer to the widget made in another method.

在您的示例中,您可能会说mt"是 控制变量.
答案仍然是创建一个类,以便您可以使用 self.前缀.

In your example it looks like you might be saying 'mt' is a control variable.
The answer would still be to make a class, so that you can use the self. prefix.

除非您需要,否则可能不需要控制变量
当您更改 Entry 小部件的内容时要更新的标签:

The control variable likely isn't necessary unless you would want
the label to be updated as you changed the contents of the Entry widget:

import Tkinter as tk

class GUI(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        bF = tk.Frame(self, bd=8, relief='sunken')
        bF.pack(expand='true', fill='x')

        var = tk.StringVar()
        var.set('Hello')
        entryLabel = tk.Label(self, textvariable=var)
        entryLabel.pack()

        mEntry = tk.Entry(self, bd=4, relief='sunken', textvariable=var)
        mEntry.pack()

gui = GUI()
gui.mainloop()

这篇关于在 Tkinter 中单击按钮时如何调用操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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