How to call an action when a button is clicked in Tkinter

2024/10/12 14:16:27

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 withif (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.

Answer

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

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.

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.

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 tkclass 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()
https://en.xdnf.cn/q/118192.html

Related Q&A

Access range of elements from an array Python

Considering the following dataset:>>> data[:10] array([(T, 2, 8, 3, 5, 1, 8, 13, 0, 6, 6, 10, 8, 0, 8, 0, 8),(I, 5, 12, 3, 7, 2, 10, 5, 5, 4, 13, 3, 9, 2, 8, 4, 10),(D, 4, 11, …

Python - Remove extended ascii

Okay, so I am new to the whole python world so bear with me. Background: We are trying to offload logs into mongo to be able to query and search for them quicker. The device already prints them in a de…

Selenium - Python - Select dropdown meun option - No ID or Name

I am trying to select and element in a dropdown menu:The HTML is:<div class="col-lg-6"><select data-bind="options: indicator_type_list,value: indicatorType,optionsCaption: Choos…

How to prevent triples from getting mixed up while uploading to Dydra programmatically?

I am trying to upload some data to Dydra from a Sesame triplestore I have on my computer. While the download from Sesame works fine, the triples get mixed up (the s-p-o relationships change as the obje…

Adding a new row to a dataframe in pandas for every iteration

Adding a new row to a dataframe with correct mapping in pandasSomething similar to the above question.carrier_plan_identifier ... hios_issuer_identifier 1 AU…

Twisted client protocol - attaching an interface frontend

I am following the tutorial on writing a client/server pair in Twisted located here:http://twistedmatrix.com/documents/current/core/howto/clients.htmlI have everything working for the communication of …

Get query string as function parameters on flask

Is there a way to get query string as function parameters on flask? For example, the request will be like this.http://localhost:5000/user?age=15&gender=MaleAnd hope the code similar to this.@app.…

cython.parallel cannot see the difference in speed

I tried to use cython.parallel prange. I can only see two cores 50% being used. How can I make use of all the cores. i.e. send the loops to the cores simultaneously sharing the arrays, volume and mc_vo…

Is it possible (how) to add a spot color to pdf from matplotlib?

I am creating a chart which has to use (multiple) spot colors. This color could be one that is neither accessible from RGB nor CMYK. Is there a possibility to specify a spot color for a line in matplot…

Separate keywords and @ mentions from dataset

I have a huge set of data which has several columns and about 10k rows in more than 100 csv files, for now I am concerned about only one column with message format and from them I want to extract two p…