Purpose: Multi menu program that when a mode is selected it will execute that mode indefinitely within it's own loop with a visible timer of prefixed time for example 60sec. It will be used in a Raspberry Pi to control some automation. I have succeeded in making everything except the timer. I tried with tk timer, countdown, whiles and fors, with partial or no success. It's probably due to my inexperience and the fact that I'm not clear about when or where the variables are declared.
Any help is appreciated, code follows.
import tkinter as tk
from tkinter import *
import sys
import os
import time
if os.environ.get('DISPLAY','') == '':
print('no display found. Using :0.0')
os.environ.__setitem__('DISPLAY', ':0.0')
def mode1():
print("Mode 1")
#do stuff
def mode2():
print("Mode 2")
#do stuff
def mode3():
print("Mode 3")
#do stuff
master = tk.Tk()
master.attributes('-fullscreen', True)
master.title("tester")
master.geometry("800x480")
label1 = tk.Label(master, text='Choose Mode',font=30)
label1.pack()
switch_frame = tk.Frame(master)
switch_frame.pack()
switch_variable = tk.StringVar()
off_button = tk.Radiobutton(switch_frame, bg="red", text="Off", variable=switch_variable,
indicatoron=False, value="off", width=20, command=quit)
m1_button = tk.Radiobutton(switch_frame, selectcolor="green", text="Mode 1", variable=switch_variable,
indicatoron=False, value="m1", width=20, height=10, command=mode1)
m2_button = tk.Radiobutton(switch_frame, selectcolor="green", text="Mode 2", variable=switch_variable,
indicatoron=False, value="m2", width=20, height=10, command=mode2)
m3_button = tk.Radiobutton(switch_frame, selectcolor="green", text="Mode 3", variable=switch_variable,
indicatoron=False, value="m3", width=20, height=10, command=mode3)
off_button.pack(side="bottom")
m1_button.pack(side="left")
m2_button.pack(side="left")
m3_button.pack(side="left")
timertext = tk.Label(master, text="Next execution in:")
timertext.place (x=10, y=150)
#timerlabel = tk.Label(master, text=countdown)
#timerlabel.place (x=200, y=150)
master.mainloop()
I tried including this timer to my script, but instead of showing the timer in a separate window, I tried to include it in the parent window.
import tkinter as tk
from tkinter import *
import sys
import os
import time
class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.label = tk.Label(self, text="", width=10)
self.label.pack()
self.remaining = 0
self.countdown(10)
def countdown(self, remaining = None):
if remaining is not None:
self.remaining = remaining
if self.remaining <= 0:
self.label.configure(text="Doing stuff!")
self.update_idletasks()
time.sleep(0.3)
os.system('play -nq -t alsa synth {} sine {}'.format(0.5, 440))
#do stuff
self.remaining = 10
self.countdown()
else:
self.label.configure(text="%d" % self.remaining)
self.remaining = self.remaining - 1
self.after(1000, self.countdown)
if __name__ == "__main__":
app = ExampleApp()
app.mainloop()
`