Tkinter using widget.after(30000, lambda: showFirstScreen() - python

I have a GUI with alot of buttons/widgets. I want that if 30 secs passes and the user didnt hit anything on the GUI that it will automaticly go back to the first screen.
I added this between root = Tk() and root.mainloop()
root.after(30000, lambda: showFirstScreen())
This line will go to my first screen after 30 seconds even if you hit the screen. So there is no counter that resets after hitting a button/widget.
I try'd to add it to a specific button on a other window so:
button13.after(30000, lambda: showFirstScreen())
If I reach the page with the button it will move to the main window after 30 seconds. But If I go back to the button it wont repeat the action above.
Any idea what im doing wrong?

You could use a variable to check if the user interacts with widgets, something like,
def showFirstScreen(buttons_pressed):
if buttons_pressed==True:
...code to go to first screen...
Use the buttons_pressed variable inside all your buttons' call methods to check if the user has used any button.
If you want to add after() to a button on another window, you can use a separate function,
button13_onPress():
if buttons_pressed==True:
root.after(30000, lambda: showFirstScreen())

Related

Tkinter Closing Frame After Button Click

I am trying to clear my Tkinter frame a few seconds after I click a button. Currently, this program works fine. It runs, and when the button is clicked, the frame is cleared. However, I want to make it so that after the button is clicked, the frame stays there for x number of seconds longer. After the time is up, the frame is cleared in the same way as it currently does (my clearFrame function). The commented line - I thought the .after would accomplish this but it just makes the popup label show after x seconds instead. I've seen this page but couldn't successfully apply it to what I want to do. Unlike what that page shows, I don't want to destroy my window or my frame, I just want to run my clearFrame function after x seconds.
#runs when the button is clicked, the button is on frame1
def click():
top, top_width, top_height = createFrame()
createPopUpLabel(top, "Ok")
autoClose(top, 5) #automatically close my pop up label
#frame1.after(2000, clearFrame(frame1))
clearFrame(frame1)
frame1.after(2000, clearFrame(frame1))
after takes a callback function, which should just be a reference to a function e.g. clearFrame.
That's why your function just executes immediately, you need to pass lambda: clearFrame(frame1)

[PyQt]Open second window every time i push the button without closing the program

This is my main program. I run it and it opens me my main window with a pushbutton. When i push the button it opens me a second window. When i close the second window and push the button again, the second window won't open. I have to run the program again to work. How should I do to open my second window every time i press the button from the main window,without closing the main window/program?
You only call show() when Adauga_p_camera is None. After the first click it is no longer None. You need to move the call to show() outside of the conditional.
def Adauga_p_camera(self):
if self.Adauga_p_camera is None:
self.Adauga_p_camera = Adauga_p_camera(self)
self.Adauga_p_camera.show()
You also reuse the name Adauga_p_camera as the name of a class, method and instance variable. Not only is this confusing, but in the case of the latter 2 you are actually overwriting the method definition when you write self.Adauga_p_camera=None. You should assign each of these items a unique name to avoid any unintended behaviour.

Tkinter Initial Cursor position and Enter to press button

I am making a box that is similar to the tkMessageBox. There are two simple behaviors that I want to the box to have. First I want the button to be selected automatically when the window opens, and second I want to be able to press enter to push the button. Sounds simple and I realize that I could use the tkinterMessageBox to do this same thing, but this is a stepping stone, and I would like to know how to do this in the future for other things.
The current behavior of the window below is that it opens, and if I press tab it will select the button, but then i can only press the button with the mouse. Again the desired functionality is to have the button selected right away and be able to press the button with the enter key.
import Tkinter, tkMessageBox
from Tkinter import *
def closewindow():
Messagebox.destroy()
Messagebox=Tk()
l3=Label( Messagebox, text="This is your preview! Align camera then press ESC")
b3=Button(Messagebox, text="Okay", command=closewindow)
l3.grid(row=1,column=1)
b3.grid(row=2,column=1)
Messagebox.mainloop()
You can actually do this with just two lines of code:
b3.bind('<Return>', lambda _: closewindow())
b3.focus_set()
The first binds the button to the Enter key and the second sets the application's focus on the button.
Note that I had to use a lambda with the binding to handle the event object that will be sent to the callback. You could however change the definition of closewindow to handle this:
def closewindow(event=None):
Messagebox.destroy()
Now you can just do:
b3.bind('<Return>', closewindow)
For more information on bindings in Tkinter, see Events and Bindings over on Effbot.

Python Tkinter: Changing other button's text from event handler of one button

I have the following problem when using tkinter to create a very simple window containing a matrix of buttons: When one of the buttons is clicked, the event handler changes the text of that button using the configure method on the button widget. This works. But I also want to change the text in one of the other buttons, which does not work. The method I use is that on creating the button, I store the object returned by the Button method before I use the grid geometry manager to place it. This object looks like ".123456789L" when printed and seems to be a pointer to the widget. I also use configure on this to change the button text. But somehow it seems to be wrong, because it works sometimes, and most of the times not. There's unfortunately no error message, just nothing happens when calling configure. I checked and it seems to be the correct pointer to the widget. Do I have to use a special way to affect a widget other that the one that called the event handler? These are the relevant parts of the code:
# CREATING THE BUTTONS:
buttons={} # global
for i in range(3):
for j in range(3):
button = Tkinter.Button(self,text='foo')
buttons[button]=(i,j)
button.grid(column=j,row=i)
button.bind( "<Button-1>", self.OnButtonClick )
# CHANGING BUTTONS:
def find_button(i,j):
"""Return the pointer to the other button to be changed when a button has been clicked."""
for button,key in buttons.items():
if key==(i,j): return button
def OnButtonClick(self,event):
print "You clicked the button",buttons[event.widget]
i,j=buttons[event.widget]
old_button=find_button(i,j) # This is simplified, I don't actually pass i,j, but other values. But I checked this and it returns the reference to the correct button. But this simplified version works the same way, just assume a different button that the one pressed would be returned.
old_button.configure(text = 'blabla') # THIS DOES NOT WORK
event.widget.configure(text = 'something') # THIS WORKS
I have the same problem and i solve it with:
buttons[button]=(i,j,button)
and in the function OnButtonClicK:
i,j,old_button=buttons[event.widget]
old_button.configure(text = 'blabla') # THIS DOES NOT WORK

getting the label value of a button in Gtk

Am learning Gtk. I wanted to build a calculator, in which i want to display the number pressed , in the textbox. I have completed it, by calling different functions for different buttons clicked, and appending the value in the textbox with the value of the button pressed. Using python 2.7.3
Is there a way to obtain the label value of the button pressed so that i can use a single function instead of 10 functions from 0 to 9?
Thanks in advance
Button callbacks include the widget itself, and you can also pass data. See here.
instead of reading the label of the GtkButton, which is pretty much error prone, you should associate the value represented by the button to the button instance itself, e.g.:
button = Gtk.Button(label='1')
button._value = 1
# add button to the container
button.connect('clicked', on_button_clicked)
button = Gtk.Button(label='2')
button._value = 2
# add button to the container
button.connect('clicked', on_button_clicked)
and then read the value from the button instance inside the signal handler, e.g.:
def on_button_clicked(button):
print 'you pressed the button of value: %d' % (button._value)
GtkWidget instances in Python are Python object, and thus behave like any other Python object.

Categories

Resources