I am new to python development and am currently building a gui using tkinter.
My button works as expected when clicked, opens another window displays face bounding boxes, however when I close the faces window and return to the buttons main window, the button still displays as clicked (pressed/sunken) and I cannot click any other buttons or close the window.
How do I release the button after it is clicked?
def btn1():
os.system("python App.py group.jpg")
button1 = Button(window, text = "Button 1", command=btn1).grid(row=0, column=0)
(SOLUTION)
I discovered that the last line of the App.py script was: cv2.waitKey(0)
The user must hit "Enter" in-order for the commmand/waitKey to terminate.
Closing the window using the 'X' on the window toolbar, does close the window but does not terminate the command and leaves the button in a pressed-state.
I have amended this value to specific time parameter 2500 i.e. 2500 milliseconds so no user involvement is required. Although as stated, simply pressing 'enter/return' terminates the command and releases the button.
To confirm, this works for both os.system and subprocess.
Hope this helps save others sometime, and thanks to everyone for your help.
Related
I have a button that, when pressed, changes the page by degridding its parent frame and gridding new frames. However, if the user hits space after this button is pressed, it runs the button's command again because the button is still highlighted/selected even though it is no longer visible. This presents some big problems for my code which depends on this button only being pressed once. How can I "clear" the highlighting of a button or at least disable the spacebar from being used to activate buttons?
I have a pop-up menu that comes up when I right click on a widget in tkinter
def popup(event):
self.popup_menu.tk_popup(event,x, event.y, 0)
popup_menu = tk.Menu(widget, tearoff=False)
popup_menu.add_command(label='whatever', command=lambda: print('whatever'))
...
bind('<Button-3>', popup)
It pops up fine, but a command runs even when it is right clicked. I would like to make the command run only when it is left clicked, to prevent weird behavior when releasing the right button.
I've looked through the options here, and there's not too much information elsewhere about pop-up menues. If anyone knows how to disable right-click or a work around pop-up where I can control things like that.
EDIT: my current work around is binding the popup to the release of the right button, so the release can't trigger an event. bind('<ButtonRelease-3>', popup)'
I am trying to activate a function in python which will save a timestamp to a file. However, I wish to activate that function after the first mouse click that accurses outside the Tk window.
For example, after running the program I will minimize it and press on the chrome browser icon and while doing so my program will log the time stamp.
I tried to use the bind function but it works for clicks that accrue only in the Tk window
By the way, I am currently using Tkinter as my GUI platform however if there is a way to do it with another libraries please share and I will adjust my program
thanks :)
Use the <FocusOut> event:
import tkinter as tk
root = tk.Tk()
def focus_lost(event):
print("Clicked outside the window")
root.bind("<FocusOut>", focus_lost)
root.mainloop()
If you are focussed on the tkinter window (e.g. if you have clicked on it), then when you click elsewhere it will detect this as a <FocusOut> event.
I have a Python/Tkinter app with a bunch of Buttons and I want to give the user an ability to edit the text on these buttons.
The most intuitive approach to me is to create an Entry widget on top of the button if the user right-clicks the button, and temporarily hide the button.
I did that and it's somewhat working but there is a problem. The button does not want to stay hidden. E.g. I create an Entry widget and it initially appears on top of the Button. Then, if I click inside the entry box, it briefly disappears (for a second or two), showing the button again, and then reappears. If I hit 'tab' and move focus out of the Entry, it disappears completely (exposing the button again) and only reappears when I click back into the entry/button area.
Am I doing something wrong / unintended, is that a bug, and is there a workaround?
Here's my Entry creation code ('self' is the button widget):
def on_mouse_rightclick(self, event):
self.prev_state = self["state"]
self.entry = Entry(self, {"width":8})
self.entry.pack(side=TOP, anchor=W)
self.entry.insert(0, self['text'])
self.entry.bind('<Return>', self.on_entry_edit)
self.entry.bind('<Escape>', self.on_escape)
self['state'] = DISABLED
self.lower(self.entry)
I tried to call self.entry.lift() and even to unbind '<Button-1>' temporarily from the button widget, with no effect.
I'm using 32-bit Python 2.7.9 on Windows.
Here's an excerpt for a main menu using Python Tkinter
master = Tk()
lbl = Label(master, text = "Main Menu:")
lbl.grid(row=0,column=1)
def list():
import list
listb = Button(master, text = "Patient List", command=list, width=10)
listb.grid(row=1, column=1)
There's a button "Patient List" that when pressed, opens a window specified from a different python file.
My problem is that the Main Menu window suffers a bug where the window will collapse. The new window comes out fine, but after closing it, I can't use my Main Menu.
Also, on my other buttons. The Main Menu doesn't collapse, but let's say I have a button Add Patient and after clicking it, the Add Patient window appears. It's functional. And then I close it, either using the quit button I programmed in it, or the close button. The Main Menu won't open the Add Patient window again.
So how can I smoothly navigate from one python program to another? Without the main menu collapsing. While being able to open a window over and over again.
My Patient List program has a lot of customized gui programming in it. I think it's because I use a grid in my Main Menu, and it's not a grid in my Patient List. But it shouldn't be affecting the Main Menu because they're supposed to be separate programs.
When doing such program with TKinter I generally use Screen classes.
To your example it would be for example:
"MainScreen", "AddPatienScreen", ...
All are subclass of the Frame tkinter class.
My Tk app only display one screen and furnish function to destroy and replace one screen by another one.
I have not access to any code now, but I invite yourself to try this approach if you understand it. It lead to well separated code and had good results.
I may provide you a very short example if required.