Skip to content

Instantly share code, notes, and snippets.

@cpatdowling
Last active October 20, 2020 18:49
Show Gist options
  • Save cpatdowling/01c3165fe7f2cd38aff1c5f7ec30d181 to your computer and use it in GitHub Desktop.
Save cpatdowling/01c3165fe7f2cd38aff1c5f7ec30d181 to your computer and use it in GitHub Desktop.
Basic structure for a switch condition triggered by a GUI button, designed to change variable values within a while loop performing some set of instructions without the need for a dedicated tkinter window thread at the cost of strict concurrency of action
import tkinter as tk
flag = False # Global flag
idx = 0 # loop index
def switch():
"""Enable scanning by setting the global flag to True."""
global flag
flag = True
root = tk.Tk()
root.title("Switch GUI")
app = tk.Frame(root)
app.grid()
switch = tk.Button(app, text="Switch", command=switch)
switch.grid()
a_variable = "initial value"
while True:
#some other set of instructions, for example, listening to a directory
#and updating a_variable to a new directory to listen to
if a_variable != "initial value":
print("a_variable changed to ", a_variable)
if idx % 500 == 0: #update every half second
root.update()
if flag:
print("Button pressed")
#switch some while loop variable values
a_variable = "new value"
flag = False
idx += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment