How to disable a button in tkinter? [duplicate] - python

This question already has answers here:
Disable / Enable Button in TKinter
(2 answers)
Closed 2 years ago.
When I click a button a new button displays but I want the previous clicked button to be disabled.
import tkinter as tk
def newbutton():
newbtn = tk.Button(app, text = "New Window button")
newbtn.pack()
app = tk.Tk()
buttonExample = tk.Button(app, text="Create new window", command=newbutton)
buttonExample.pack()
app.mainloop()

hi there have you tried to use the state command like this
buttonExample = tk.Button(app, text="Create new window", command=newbutton, state=DISABLED)

You can implement a very simple function that can disable a button
def disableButton(my_button):
my_button.config(state='disabled')
There is already a post on stack talking about that here

Related

tkinter entry box updating before button is clicked [duplicate]

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

Tkinter checkbox executing command only once [duplicate]

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.
x = tk.IntVar()
def test():
print(x.get())
checkBox = tk.Checkbutton(text="test",command=test(),variable=x)
checkBox.grid(row=12,column=2)
b=tk.Button(text="test",command=test)
b.grid(row=12,column=0
this is some code I put in my program for testing, when I check the checkbox the value does change (I can confirm it with the button I made to test) but the command does not get executed.
Remove () in command
command= test
This works fine:
import tkinter as tk
win = tk.Tk()
x = tk.IntVar()
def test():
print(x.get())
checkBox = tk.Checkbutton(text="test", command=test, variable=x)
checkBox.grid(row=12, column=2)
win.mainloop()

how do I make a button in Tkinter that runs a shell command [duplicate]

This question already has answers here:
Running Bash commands in Python
(11 answers)
Closed 2 years ago.
I am trying to make a button the command: print("test"). Here is what I have so far:
import os
root = tk.Tk()
button = tk.Button(text="print",
fg="black",
command=print("test")
)
button.pack()
root.mainloop()
how can I do this?
Following the solution to this question, the following should work:
import os
root = tk.Tk()
button = tk.Button(text="print",
fg="black",
command=lambda: print("test")
)
button.pack()
root.mainloop()

Display Path of a file in Tkinter using "browse" Button - Python [duplicate]

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 6 years ago.
I have been reading through several posts regarding to Browse button issues in Tkinter but I could not find my answer.
So I have wrote this code to get a directory path when clicking the browse button, and displaying this path in an entry field.
It woks partly : a file browser window directly pops up when I run the script. I indeed get the path in the entry field but if I want then to change the folder using my Browse button it does not work.
I dont want to have the browser poping up right from the start but only when I click on Browse !
Thanks for your answers
from Tkinter import *
from tkFileDialog import askdirectory
window = Tk() # user input window
MyText= StringVar()
def DisplayDir(Var):
feedback = askdirectory()
Var.set(feedback)
Button(window, text='Browse', command=DisplayDir(MyText)).pack()
Entry(window, textvariable = MyText).pack()
Button(window, text='OK', command=window.destroy).pack()
mainloop()
This is so easy -- you need to assign the path to a variable and then print it out:
from tkinter import *
root = Tk()
def browsefunc():
filename = filedialog.askopenfilename()
pathlabel.config(text=filename)
browsebutton = Button(root, text="Browse", command=browsefunc)
browsebutton.pack()
pathlabel = Label(root)
pathlabel.pack()
P.S.: This is in Python 3. But the concept is same.

Sending a variable with a button-command (python 3.3 / tkinter) [duplicate]

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.

Categories

Resources