This question already has an answer here:
How to use os.startfile with a button command (TkInter)
(1 answer)
Closed 9 years ago.
I want to update a Tkinter label when a button is clicked.
The following code works fine:
import tkinter
from tkinter import *
window = tkinter.Tk()
v="start"
lbl = Label(window, text=v)
lbl.pack()
def changelabel():
v ="New Text!"
lbl.config(text=v)
btn=Button(window, text="Change label text", command=changelabel)
btn.pack()
window.mainloop()
But for more dynamics I would like the New text to be sent into the changelabel-function.
I've tried a lot of things. This is what I think should work, but it prints the "New dynamic text" right away, instead of waiting for my click...
import tkinter
from tkinter import *
window = tkinter.Tk()
v="start"
lbl = Label(window, text=v)
lbl.pack()
def changelabel(v):
lbl.config(text=v)
v ="New, dynamic text!"
btn=Button(window, text="Change label text", command=changelabel(v))
btn.pack()
window.mainloop()
Do you understand my error?
You need to "hide" the call to changelabel. The easiest way to do this is to use a lambda:
btn=Button(window, text="Change label text", command=lambda: changelabel(v))
Otherwise, when Python runs through your code, it sees this:
changelabel(v)
Interpreting it as a valid function call, it runs it.
Related
i've just started learning tkinter for python, and i'm trying to get the button to change its text when it's clicked on.
this seems like a very simple question, but i can't find any answers. the code i'm using at the moment doesn't work - when the window opens, it displays 'clicked!' as a label above the button immediately, before i've clicked on the button.
from tkinter import *
root = Tk()
def click():
label = Label(root, text = 'clicked!')
label.pack()
button = Button(root, text='click me', command = click())
button.pack()
root.mainloop()
To change an existing button's text (or some other option), you can call its config() method and pass it keyword arguments with new values in them. Note that when constructing the Button only pass it the name of the callback function — i.e. don't call it).
from tkinter import *
root = Tk()
def click():
button.config(text='clicked!')
button = Button(root, text='click me', command=click)
button.pack()
root.mainloop()
You're passing command = click() to the Button constructor. This way, Python executes click, then passes its return value to Button. To pass the function itself, remove the parentheses - command = click.
Is there a simple way to get the right click menu to open on texty only and not the whole window?
This was a quick mashup to illustrate my question. Inheriting from texty on line 25 was a shot in the dark, which didnt work, but it's close to a simple solution, like I am seeking. I was hoping to avoid programming a whole class each time I want to set a right click menu.
from tkinter import *
from tkinter import ttk
def menu_popup(event):
try:
popup.tk_popup(event.x_root, event.y_root, 0)
finally:
popup.grab_release()
win = Tk()
win.geometry("600x550+125+125")
e = Entry(win, width=50, font=('Helvetica', 11))
e.pack()
e.insert(0, "Some text....")
label = Label(win, text="Right-click to see a menu", font= ('Helvetica 18'))
label.pack(pady= 40)
texty=Text(win, height=10)
texty.pack()
popup = Menu(texty, tearoff=0)
popup.add_command(label="New")
popup.add_separator()
popup.add_command(label="Open")
popup.add_separator()
popup.add_command(label="Close")
win.bind("<Button-3>", menu_popup)
button = ttk.Button(win, text="Quit", command=win.destroy)
button.pack()
mainloop()
The widget on which the callback should be executed for the respective event is determined by the widget you call bind on(and the level of bind too*). So if you want the event to be identified within texty, then apply binding to it.
texty.bind("<Button-3>", menu_popup)
* There is bind_all which executes no matter which widget has focus or is called upon. Read 54.1. Levels of binding for more info.
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 1 year ago.
I am writing a GUI application in python with tkinter. I have a function that is used in the button widget to change the text of an entry widget. When I run the python script, the entry widget updates with the text before I click the button widget. Can anyone tell me why it is doing this and how to fix it?
Please see my code below:
import tkinter as tk
def changeText(TKEntry, text):
TKEntry.insert(0, text)
def buildMain():
window = tk.Tk()
window.title("Login")
window.geometry("200x110")
lblLogin = tk.Label(window, text="Log In")
lblLogin.place(x=85,y=0)
lblUser = tk.Label(window, text="Username:")
lblUser.place(x=0,y=30)
edtUser = tk.Entry()
edtUser.place(x=70,y=30)
lblPass = tk.Label(window, text="Password:")
lblPass.place(x=0,y=50)
edtPass = tk.Entry()
edtPass.place(x=70,y=50)
btnLogin = tk.Button(text="Log In", command= changeText(edtUser,"Text Changed"))
btnLogin.place(x=150, y=80)
window.mainloop()
buildMain()
You need lambda:
command=lambda:changeText(edtUser,"Text Changed")
I think when the button was defined, the function was executed. That's why the entry was updated
Good Morning/Evening,
I want to read a number from a spinbox, and if it is 2, it should print something. But my code does not work out. I've tried it with a slider instead of a spinbox and it worked out. But for me, it is really important to use a spinbox, so I hope somebody have an idea.
Code:
from tkinter import *
def a():
if spin.get()==2:
print("Hello World")
root = Tk()
root.geometry('300x100')
spin =Spinbox(root, from_=0, to=10,command=a)
button = Button(root, text='Enter')
button.pack(side=RIGHT)
spin.pack(side=RIGHT)
root.mainloop()
Adding to #coolCloud's answer I would suggest setting a textvariable for spinBox. So if the user changes it using the entry. It would automatically be updated.
Something like this:
from tkinter import *
def a(*event):
if text.get()=='2':
print("Hello World")
root = Tk()
root.geometry('300x100')
text = StringVar()
text.trace('w', a) # or give command=a in the button if you want it to call the event handler only when the button is pressed
spin =Spinbox(root, from_=0, to=10, textvariable=text)
button = Button(root, text='Enter')
button.pack(side=RIGHT)
spin.pack(side=RIGHT)
root.mainloop()
I am trying to experiment and get the button to only display the label when the button is clicked instead it is opening up another GUI window. The main frame is called secret message. Within this when i click onto the button it should then replace the empty place with the label in row=2.
Could someone explain to me how i can raise the label rather than just opening up a new window. All code is functional but i want another way around this, i am new to python.
from tkinter import *
def topLevel():
top=Toplevel()
top.geometry("300x200")
root=Tk()
root.title("Secret Message")
button1 = Button(text="Push this button to see hidden message!", width =60, command=topLevel)
button1.grid(row=1, column=0)
label1 = Label(width=50, height=10, background="WHITE", text= "There is no secret!")
label1.grid(row=2, column=0)
root.mainloop()
You question title has nothing to do with your question.
To update the geometry of your label you simple need to tell the function where you want the label on the container you set up your label in. In this case you do not define the container so the widgets default to the root window.
Here is a working example that will update the label geometry when you press the button.
from tkinter import *
root=Tk()
root.title("Secret Message")
def grid_label():
label1.config(text="There is no secret!")
Button(root, text="Push this button to see hidden message!", width=60, command=grid_label).grid(row=1, column=0)
label1 = Label(root, width=50, height=10, background="WHITE")
label1.grid(row=2, column=0)
root.mainloop()