Disable button in Tkinter (Python) - python

Hi , I have some question to ask
I just want to disable the button when i start my program
in attached image, it looks like the button is already disabled ,but its response to my click event or keyboard event
What should i do ?
Thank you for all answer
from Tkinter import *
def printSomething(event):
print("Print")
#Start GUI
gui = Tk()
gui.geometry("800x500")
gui.title("Button Test")
mButton = Button(text="[a] Print",fg="#000",state="disabled")
mButton.place(x=5,y=10)
mButton.bind('<Button-1>',printSomething)
gui.bind('a',printSomething)
gui.mainloop()

You need to unbind the event. state="disabled"/state=DISABLED makes button disabled but it doesn't unbind the event. You need to unbind the corresponding events to achieve this goal. If you want to enable the button again then you need to bind the event again. Like:
from Tkinter import *
def printSomething(event):
print("Print")
#Start GUI
gui = Tk()
gui.geometry("800x500")
gui.title("Button Test")
mButton = Button(text="[a] Print",fg="#000",state="disabled")
mButton.place(x=5,y=10)
mButton.bind('<Button-1>',printSomething)
mButton.unbind("<Button-1>") #new line added
gui.bind('a',printSomething)
gui.mainloop()

Related

How to pause window to turn on another window?

I use tkinter and CTK:
I have created a page for login and I want to stop or use this page when the user is logged in, I want to show a new window and I want to resume the window when I want? How can I do that, I didn't know how to make it
I'll bite. Here's an example application that opens a second window when the user clicks a button on the main window, and disables interaction with the main window until the second window is closed.
Some configuration has been omitted for brevity, and I'm not using CTk here because I don't know how you've implemented it in your specific application - however, it should be easy enough to modify this example to work with CTk.
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.open_button = ttk.Button(
self,
text='Open 2nd Window',
command=self.modal
)
self.open_button.pack()
def modal(self):
self.window = tk.Toplevel(self) # create new window
# bind a handler for when the user closes this window
self.window.protocol('WM_DELETE_WINDOW', self.on_close)
# disable interaction with the main (root) window
self.attributes('-disabled', True)
self.close_button = ttk.Button(
self.window,
text='Close Modal',
command=self.on_close
)
self.close_button.pack()
def on_close(self):
# re-enable interaction the root window
self.attributes('-disabled', False)
# close the modal window
self.window.destroy()
if __name__ == '__main__':
app = App()
app.mailoop() # run the app
In the future:
Provide code that shows you made a good-faith effort to solve the problem on your own
Don't post the same question multiple times within hours - if you need to make changes, edit the original question
If you mean you want to open up another window to do something before going back to the original window, you should consider using message box. Here is a link that goes over the types of messageboxes: https://docs.python.org/3/library/tkinter.messagebox.html.

Is there a click event handler in tkinter?

Guys I'm writing a GUI in tkinter. I wanted to handle click events like whenever a user use the 'left' mouse button on the GUI. This is for playing a sound when a user clicks. So is there any functions like:
onClick(lambda: play()) #call a function
Thanks in advance :)
You can add a click event to a canvas.
Something like this should work:
root = Tk()
def on_click(event):
print("you clicked")
canvas = Canvas(root, width=800, height=600)
canvas.bind("<Button-1>", on_click)
canvas.pack()
# Canvas.focus_set is required if the window already contains a widget that has keyboard/input focus
canvas.focus_set()
root.mainloop()
Here are some examples of using this method: https://python-course.eu/tkinter/events-and-binds-in-tkinter.php

Python- Tkinter Radio Button changes not being registered

I am trying to use tkinter to create a basic program. It is not capturing my radio button input.
It should display a screen with a title and a next button. If the next button is pressed it should bring up another window with two radio buttons and a next button. If that one is pressed it should bring up a third screen telling you what you choose. Currently, it tells you that you chose nothing no matter what
from tkinter import *
from tkinter import ttk
from functools import partial
def forward():
root2=Tk()
mainframe2=ttk.Frame(root2)
mainframe2.grid(row=0,column=0)
pick=StringVar()
ttk.Radiobutton(mainframe2, text='A', variable=pick, value='A').grid(row=1,column=1)
ttk.Radiobutton(mainframe2, text='B', variable=pick, value='B').grid(row=2,column=1)
#What I have tried
#ttk.Button(mainframe2, text='Next',command=lambda: advance(pick)).grid(row=3,column=1)
ttk.Button(mainframe2, text='Next',command=partial(advance,pick)).grid(row=3,column=1)
#ttk.Button(mainframe2, text='Next',command=advance).grid(row=3,column=1)#if you this and def
advance(args) it says pick is undefined
root2.mainloop()
def advance(pick):
choice=pick.get()
screen3=Tk()
mainframe3=ttk.Frame(screen3)
mainframe3.grid(row=0,column=0)
result=str('You Chose '+choice)
ttk.Label(mainframe3, text=result).grid(row=1,column=1)
screen3.mainloop()
root = Tk()
mainframe=ttk.Frame(root) #create a screen
mainframe.grid(row=0,column=0)
ttk.Label(mainframe, text='Welcome to my Broken Program').grid(row=1,column=1)
ttk.Button(mainframe, text='Next',command=forward).grid(row=2,column=1)
root.mainloop()

python tkinter - get cursor in new window

I'd like to use the tab key in both showing of the following code:
from tkinter import *
main = Tk()
def pressButton():
main.destroy()
End=Button(main,text='Finished',width=15,command=pressButton).grid()
main.mainloop()
from tkinter import *
main = Tk()
def pressButton():
main.destroy()
End=Button(main,text='Finished',width=15,command=pressButton).grid()
main.mainloop()
first window works: i can press tab and space and it opens the second window; there i'm not able to "press button" by using tab and space, because the cursor is in Python Shell.
How can I get the cursor in the second window?
As mention in that post "Tkinter main window focus", it is possible to force the focus to the main window.
Solution - add a call to the after() to focus_force().
from tkinter import *
main = Tk()
def pressButton():
main.destroy()
End=Button(main,text='Finished',width=15,command=pressButton).grid()
# call the focus_force() after the window is displayed
main.after(1, lambda: main.focus_force())
main.mainloop()

Automatically Activate Main Window in TkInter

Is it possible to automatically activate the main window of a tkinter app? I am using Yosemite on a Mac. When the window comes up, the title bar is grayed out, and I have to click on the window before it will respond to events. The Tk manual says that event generate, "Generates a window event and arranges for it to be processed just as if it had come from the window system." I tried generating a <Button-1> event, but it had no effect. The manual goes on to say, "Certain events, such as key events, require that the window has focus to receive the event properly." I tried focus_force, but it didn't work either.
Is it possible to do what I want? Is this a Mac peculiarity? In the code below, the text changes as the mouse cursor enters and leaves the label, but the app is unresponsive until you click on the window.
import tkinter as tk
root = tk.Tk()
def visit(event):
kilroy['text'] = 'Kilroy was here.'
def gone(event):
kilroy['text'] = 'Kilroy has left the building'
def startup():
root.focus_force()
root.event_generate('<Button-1>')
frame = tk.Frame(root, width=500,height=100)
kilroy = tk.Label(frame, text="Kilroy hasn't been here.", width = 50)
kilroy.grid(row=0,column=0)
frame.grid(row=0,column=0)
kilroy.grid_propagate(0)
frame.grid_propagate(0)
kilroy.bind('<Enter>', visit)
kilroy.bind('<Leave>', gone)
root.after(100,startup)
root.mainloop()

Categories

Resources