Wait for button click - python

I'm trying to create a GUI that will have some preset test to run. Depending on what the user's selection is a secession of test will run. I'm trying to figure out what is the best way to run a test in a thread and then wait until the user presses the next button to continue.
The current way the program knows what test to run is by create a dictionary like so
A = {0:[0,0,0],1:[1,0,1],2:[0,1,1]}
the key would represent the index of a combo box and the list represent whether the test runs or not, so 0 mean don't run that particular test and 1 mean do. So, I would have a for loop that would run through the list and if it's 0 it goes to the next list element, and if it's 1 it configures the test runs it and then I would like it to wait until the user presses the next button in the GUI.
EDIT: Instead I implemented a state machine method, using Qtimer. So the GUI will stay in the wait state until the GUI send sends a signal to move from the wait sate to the next state after a button has been pressed

You can do it simple like this: first, disable next button in your GUI using yourNextButton.setDisabled(1); second, enable it at the end of your test yourNextButton.setDisabled(0) (I assume it is one method); with the button enabled,which means your test is done, you can click it and connect it with next operation you need to be done (next test or whatever), but do not forget to disable it again when it's clicked.
If you need different behavior feel free to ask.

Related

How to execute a command with a button and delete it before the command ends ? Python / Tkinter

I've got an interface where there is a 'Start' button. It runs a 'main' command where it starts a loop to run some measurements on a powermeter. I want to be able to click on an 'OK' button every time the measure is ready to be done. This button would replace the 'Start' button.
But when I try to destroy the button (buttonStart.destroy()) and then run the command main()
, the command executes but doesn't delete the button until the very end.
I've tried using threads from threading package, but it doesn't really work.
If there is a way to both destroy the button and run a command, I would be very interested !
Thanks a lot
The event loop must be given the opportunity to process events in order for the window to be removed from the screen.
There are a couple of ways to make that happen. The first is to call the update_idletasks method on any widget. That is usually enough to process the event related to the destruction of the widget, though it may be necessary to call update instead. Calling update should be avoided if at all possible. My rule of thumb is to start with update_idletasks. If that doesn't work, switch to update or switch to using after.
def my_custom_function():
startButton.destroy()
root.upddate_idletasks()
main()
The second method is to run your main function via after or after_idle. This will let tkinter naturally process all pending events before starting main. I would argue that this is the best approach. I recommend trying after_idle first, and if that doesn't work, switch to after with a small delay.
def my_custom_function():
startButton.destroy()
root.after_idle(main)

How do I use two mouse press functions in python?

First scene with first mouse press
That first onMousePress function makes the background change.
Second scene with second mouse press
This second onMousePress function is planned to make the barrels disappear but it just kind of replaces the first onMousePress and makes it so that's the only one that works. Also ignore the 2 and the fact that it actually makes a rectangle, the 2 made it not work so I can click on the future tp and the rect is for testing purposes
You can set flag of first click and some small wait time. If the event is triggered second time it is double click, otherwise it is single click when time runs out. You need to set wait time in parallel thread or use something like timer. And don't forget to reset your flag everytime.

Python Tkinter Command Click

I need to bind command button-click to a function in python. I have already bound the flag function to right click, but this only work when right-clicking with a mouse. As I write the majority of my code on a laptop, this is horribly inconvenient. Here is what I currently have:
# set up the mouse click listeners
self.bind('<Button-1>',self.expose)
self.bind('<Button-2>',self.flag)
#this is where I want to bind self.flag to command click
I'd like to use self.bind if possible, and simply bind command click to self.flag. Is it possible to do this?
You can just use:
self.bind("<Command-Button-1>",self.flag)
This can be done in addition to <Button-2> and you may want to also bind <Control-Button-1> for compatibility.
I would normally link to http://infohost.nmt.edu/tcc/help/pubs/tkinter/event-modifiers.html but it seems to be down at the moment, basically there are a few modifiers that can be used in combination with Button or Key:
Alt True when the user is holding the alt key down.
Any This modifier generalizes an event type. For example, the event pattern '<Any-KeyPress>' applies to the pressing of any key.
Control True when the user is holding the control key down.
Double Specifies two events happening close together in time. For example, <Double-Button-1> describes two presses of button 1 in rapid succession.
Lock True when the user has pressed shift lock.
Shift True when the user is holding down the shift key.
Triple Like Double, but specifies three events in rapid succession.
So you could for example bind <Control-Shift-Double-Button-1> if your program needed to be that elaborate.

Preventing a Button Press From Registering TKInter/Python 3.3

I'm in the process of creating a filling program with Python 3.3/Tkinter and have run into an issue that's really stumping me. Basically, I want to prevent button presses from registering while certain processes are taking place. I tried changing the button's state to state='disabled' but this only delays the click from registering until after the function is finished running. In other words, despite the button being "disabled", if it's clicked, the button press will register as soon as it's re-enabled. Please see the example code below.
def Button_Action:
Button.config(state='disabled')
#Activate some hardware that takes a few seconds.
Button.config(state='normal')
So, the question is: How can one selectively ignore button presses in Tkinter/Python 3?
I'm really new to Python and have tried searching for relevant questions to no avail, so please forgive me if this is a stupid question, or has been asked before. Also, I have tested this with both Radiobuttons as well as, standard Buttons (in case that helps).
You can use update method.
def button_action():
b.config(state='disabled')
b.update() # <----------
# Activate some hardware that takes a few seconds.
b.update() # <----------
b.config(state='normal')
The first call to update is to make the button displayed as disabled state.
The second call to update is to handle all pending event (clicks in your case) before enable the button.
BTW, it's normal state, not enabled that make the button back to normal state.

Python Notebook run automatically periodically

I have a script in a notebook browser that does some live data analysis, that I would like to run periodically by itself once every few seconds/minutes, rather than I having to do Ctrl+Enter all the time.
Is there any way to achieve this? Do I need any additional plug-ins, and which ones
Simplest way to do it:
import time
def periodic_work(interval):
while True:
#change this to the function you want to call, or paste in the code you want to run
your_function()
#interval should be an integer, the number of seconds to wait
time.sleep(interval)
To run once per minute, run a new cell with periodic_work(60) -- you should see a closed circle in the top right of the IPython Notebook that indicates the Kernal is busy. If/when you want to stop the live update, hit the Stop button (labeled Interrupt) on the menu bar and wait a second or so. To start it up again, run the cell that calls periodic_work again.
One way would be to make your cell a function. In another cell, just call time.sleep () in an appropriate loop. You could also have it check for the existence of some external resource to quit, if you don't want to loop an a priori known finite number of times.

Categories

Resources