I am working on my first app, and have been following some tutorials on how to setup a working GUI with tkinter and python. My application starts by playing a splash screen animation, and once it completes, switches to the "UserSetup" frame. I am still busy trying to re-code the function that plays this video so that I can call upon it as a method (or whatever works). What I wanted to know is, if there is a way to check once the thread which plays this video has stopped? Any other animations played now run concurrently, and I don't want that. I basically want it to only continue with the next set of animations or whatever once the splash screen animation has stopped.
It is quite a bit of code....
# ======================
# imports
# ======================
import tkinter as tk
import imageio
import threading
from PIL import Image, ImageTk
LARGE_FONT = ("Segoe UI", 18)
# =======================
# INITIALISATION - MAIN
# =======================
class MyApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side='top', fill='both', expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (SplashScreen, UserSetup, TimerSetup):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame(SplashScreen)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
# ======================
# PAGES
# ======================
""" To add new pages, just create a page class which inherits from tk.Frame,
add it to the dictionary, and add buttons for navigation """
class SplashScreen(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
video_name = "img//splash.avi" # This is your video file path
video = imageio.get_reader(video_name)
def stream(label):
for image in video.iter_data():
frame_image = ImageTk.PhotoImage(Image.fromarray(image))
label.config(image=frame_image)
label.image = frame_image
controller.show_frame(UserSetup)
def forget():
my_label.pack_forget()
my_label = tk.Label(self)
my_label.pack()
my_label.after(ms=15000, func=forget)
thread = threading.Thread(target=stream, args=(my_label,))
thread.daemon = 1
thread.setName("splash")
thread.start()
print(thread.getName())
class UserSetup(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
class TimerSetup(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
# ======================
# INITIALISATION (MAIN)
# ======================
app = MyApp()
#app.overrideredirect(True)
app.mainloop()
I have used is_alive() in the past.. In the form of:
while thread_name.is_alive():
pass
This will wait for the thread to finish before continuing onto the next part of your script.
Hope this helps!
Luke
Related
I'm relatively new to Tkinter and Python and just started with Tkinter in object oriented way.
Im trying to change the background colour of all the different pages I have so i have the following code
import tkinter as tk
import sqlite3
from tkinter.ttk import *
from tkinter import *
LARGE_FONT = ("Verdana", 12)
HEIGHT = 700
WIDTH = 800
class programStart(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1, minsize=WIDTH)
container.grid_columnconfigure(0, weight=1, minsize=HEIGHT)
self.frames = {}
for F in (StartPage, Register, LoginPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise() #Raises to front
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
I have tried container.configure(bg='red') and so on, to no success
How can I go about this issue?
Try this:
import tkinter as tk
import sqlite3
from tkinter.ttk import *
from tkinter import *
LARGE_FONT = ("Verdana", 12)
HEIGHT = 700
WIDTH = 800
class programStart(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1, minsize=WIDTH)
container.grid_columnconfigure(0, weight=1, minsize=HEIGHT) #0 minimum, weight is priority
self.frames = {}
for F in (StartPage, Register, LoginPage):
frame = F(container, self, bg="red")
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise() #Raises to front
class StartPage(tk.Frame):
def __init__(self, parent, controller, bg=None, fg=None):
tk.Frame.__init__(self, parent, bg=bg=, fg=fg)
# Make sure that all of the tkinter widgets have `bg=bg=, fg=fg`
Basically you need to tell all of the widgets that you are creating that the background should be red. When creating your widgets you can pass in the bg parameter (background).
This is a minimal version of the system I use to give the user of the GUI the ability to change the colors and fonts of all the widgets in the app according to color schemes he can choose himself.
import tkinter as tk
formats = {
'bg' : 'black',
'fg' : 'white'
}
class Labelx(tk.Label):
def __init__(self, master, *args, **kwargs):
tk.Label.__init__(self, master, *args, **kwargs)
def winfo_subclass(self):
''' a method that works like built-in tkinter method
w.winfo_class() except it gets subclass names
of widget classes custom-made by inheritance '''
subclass = type(self).__name__
return subclass
class Label(Labelx):
'''
If this subclass is detected it will be reconfigured
according to user preferences.
'''
def __init__(self, master, *args, **kwargs):
Labelx.__init__(self, master, *args, **kwargs)
self.config(
bg=formats['bg'],
fg=formats['fg'])
class LabelNegative(Labelx):
'''
If this subclass is detected it will be reconfigured
according to user preferences.
'''
def __init__(self, master, *args, **kwargs):
Labelx.__init__(self, master, *args, **kwargs)
self.config(
bg=formats['fg'],
fg=formats['bg'])
def change_colors():
for widg in (lab1, lab2):
if widg.winfo_class() == 'Label':
if widg.winfo_subclass() == 'Label':
widg.config(bg=formats['fg'], fg=formats['bg'])
elif widg.winfo_subclass() == 'LabelNegative':
widg.config(bg=formats['bg'], fg=formats['fg'])
root = tk.Tk()
f1 = tk.Frame(root)
f1.grid(column=0, row=0)
lab1 = Label(f1, text='Label')
lab1.grid()
lab2 = LabelNegative(f1, text='LabelNegative')
lab2.grid()
button = tk.Button(root, text='colorize', command=change_colors)
button.grid()
root.mainloop()
I am having a problem Disabling Menu's in my Tkinter App. Only I want to show them only on certain pages, I have tried to disable them in the init function of my app, but that didn't work, I have tried to disable them in the show frames function of my app but that didn't work and I have tried to disable them through the start page class of my app, in that I have tried to use self.menubar , parent.menubar and controler.menubar; But nothing seems to work. I would just code them on each individual page but this is the only way I found to even show them on any page. Any help would be much appreciated.
class App(tk.Tk):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self,*args,**kwargs)
container = tk.Frame(self)
container.pack(side='top',fill='both',expand= True)
container.grid_rowconfigure(0,weight = 1)
container.grid_columnconfigure(0,weight = 1)
menubar = tk.Menu(container)
file = tk.Menu(menubar, tearoff = 0)
file.add_command(label='Exit', command = quit)
menubar.add_cascade(label='File',menu=file)
tk.Tk.config(self, menu=menubar)
self.frames = {}
for F in (StartPage):
frame = F(container,self)
self.frames[F] = frame
frame.grid(row=0,column=0,sticky='nsew')
page = F
self.show_frame(StartPage)
def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
menubar.add_cascade(label='File',state=DISABLED)
label = ttk.Label(self, text='Start', font = LARGE_FONT).pack()
main = App()
main.mainloop()
This isn't a tkinter problem, it's how python works - to modify an object you need a reference to the object. This is true for tkinter widgets just as it is true for dictionaries, strings, or any other object in python.
In this case the object is part of the main application. You first need to save a reference to it:
class App(tk.Tk):
def __init__(self,*args,**kwargs):
...
self.menubar = tk.Menu(container)
...
In later code, you can now access this menu from the controller variable, which is a reference to the instance of App:
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
controller.menubar.entryconfigure('File',state="disabled")
However, that likely won't do what you want since that code runs when the program starts up. I'm guessing you want the code to run when the page is selected. To do that, follow the instructions in this answer to the question How would I make a method which is run every time a frame is shown in tkinter
1st lets clean up this to reflect the PEP8 standard more closely for readability reason.
Added in the imports as those should have been in your example.
We need to replace the parenthesis in (StartPage) with brackets like this [StartPage]. This is because a single value in parenthesis is treated like a single value without parenthesis so you are trying to iterate over an object and not a list.
You should be more specific on what you are configuring. Instead of calling tk.tk.config() do self.config(menu=self.menubar).
for your StartPage class you are not going to be able to do anything to the menubar for several reason. First you do not define the menubar as a class attribute in your main tk class. 2nd you are not properly calling your master and container so I have changed the argument names in the init to better reflect what we are working with so you can see what you need to call.
Lastly the crux if the issue for disabling the menu you need to use entryconfig()
See below cleaned up code:
import tkinter as tk
import tkinter.ttk as ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
container = tk.Frame(self)
container.pack(side='top', fill='both', expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.menubar = tk.Menu(container)
self.config(menu=self.menubar)
file = tk.Menu(self.menubar, tearoff=0)
file.add_command(label='Exit', command=quit)
self.menubar.add_cascade(label='File', menu=file)
self.frames = {}
for F in [StartPage]:
frame = F(self, container)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='nsew')
page = F
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, master, container):
tk.Frame.__init__(self, container)
master.menubar.entryconfig('File', state='disabled')
ttk.Label(self, text='Start').pack()
main = App()
main.mainloop()
I have this following code where I am calling the function from the button which takes input from the widgets. It's a function which takes about 4 minutes to run and for solving the 'not responding' problem of tkinter window, I want to get the func process running on the different core and terminate as it can be called again via the application running on mainloop with a different argument. I read the documentation of multiprocessing and Pool seemed to be the choice here but I don't know how to frame it here. Tried a few things with error.
class database(tk.Tk):
def __init__(self, *args, *kwargs):
tk.Tk.__init__(self, *args, **kwargs):
container= tk.Frame(self, width=1000, height=1000)
container.pack(side="top", fill="both", expand= True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in ( msPage): #many more pages here
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(msPage)
def show_frame(self,cont):
frame = self,frames[cont]
frame.tkraise()
def MarkS(msVar):
ms.func(msVar.get()) # func takes about 4 mins
class msPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
msVar = StringVar()
msCal = Calendar(self, selectmode='day'
textvariable=msVar).pack(fill="both", expand=True)
# button2 calls MarkS
button2 = ttk.Button(self,
text="Get Data",
command=lambda: MarkS(msVar)).pack(pady=30, padx=10)
app = database()
app.geometry("1066x568")
app.minsize(width=670, height=550)
app.mainloop()
This is a standalone example that might help you get started:
from multiprocessing import Process
class ms_var_class():
value = None
def __init__(self, value):
self.value = value
def get(self):
return self.value
# This is to simulate type(ms)
class ms_class():
process = None
# This is for simulating your long running function
def func(self, ms_var):
print(ms_var)
def MarkS(msVar):
ms.process = Process(target=ms.func, args=(msVar.get(),))
ms.process.start()
# Call ms.process.join() later
ms_var = ms_var_class("bogus")
ms = ms_class()
MarkS(ms_var)
if ms.process is not None:
ms.process.join()
Thank you Mike for commenting and helping me get towards the solution
Here's how I did it
Create a global queue
q= queue.Queue()
Defined the function MarkS within the class msPage
def MarkS(msVar):
q.put([7 , datetime.datetime.strptime(msVar.get(), '%x').strftime('%d-%b-%Y').upper(),0])
ThreadedTask(q).start()
7 is the unique number of the 9 functions that I had for each page (class)
Made another class of ThreadedTask as in the link below
Tkinter: How to use threads to preventing main event loop from "freezing"
class ThreadedTask(threading.Thread):
def __init__(self,queue):
threading.Thread.__init__(self)
q = queue
def run(self):
items = q.get()
if items[0]==7:
ms.func(items[1])
ThreadedTask(q).start() creates a constructor object and the run function automatically starts depending on the argument here which is of course the 1st element of q which is a list of 3 in my case. It is detected and the desired function is run in another thread which prevents the tkinter window from closing.
Hope this helps! :)
I'm trying to make a Tkinter window show updated data but it only pops up after 13 seconds with just the last value. I want it to pop up and change the values on screen. Mind you, the big goal of this code is to take data from a database (which updates every 3 seconds) and show the data onscreen, while running continuously, so if the answer could include some pointers on the "after" or "update" functions it would be greatly appreciated!
Here is what I have so far.
from tkinter import *
import time
class GUI(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
Tk.wm_title(self, "Main Window")
self.container = Frame(self)
self.container.pack(side=TOP, fill=BOTH, expand=TRUE)
self.container.grid_rowconfigure(0, weight=1)
self.container.grid_columnconfigure(0, weight=1)
self.frames = {}
self.frame = StartPage(self.container, self)
self.frames[StartPage] = self.frame
self.frame.grid(row=0, column=0, sticky=NSEW)
self.show_frame(StartPage)
def show_frame(self, controller):
frame = self.frames[controller]
frame.tkraise()
class StartPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.label = Label(self, text="Current ID:\n")
self.label.pack(padx=10, pady=10)
self.data_label = Label(self)
self.data_label.pack()
self.update_data()
def update_data(self):
var1 = StringVar()
for i in range(10):
var1.set(str(i))
self.data_label.config(text=str(i))
time.sleep(1)
main = GUI()
main.mainloop()
I can give you a partial answer. The reason you don't see any updates is that time.sleep() suspends the process and does not allow for tkinter to repaint the window.
You don't use the label textvariable correctly. Specify it in the label and the label will change as you change the textvariable.
You use both pack and grid at the same time which may cause problems.
I have not used after() in a class before so I don't know how to work it, but this example should give you some pointers. I'm keeping console printouts in the code so I can see what it does.
from tkinter import *
import time
class GUI(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
Tk.wm_title(self, "Main Window")
self.container = Frame(self)
self.container.pack(side=TOP, fill=BOTH, expand=TRUE)
self.frames = {}
self.frame = StartPage(self.container, self)
self.frames[StartPage] = self.frame
self.frame.pack(side=TOP, fill=BOTH, expand=TRUE)
self.show_frame(StartPage)
def show_frame(self, controller):
frame = self.frames[controller]
frame.tkraise()
frame.update_data()
print('Done')
class StartPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.parent = parent
self.label = Label(self, text="Current ID:\n")
self.label.pack(padx=10, pady=10)
self.var1 = StringVar()
self.data_label = Label(self, textvariable=self.var1)
self.data_label.pack()
self.update_idletasks() # Calls all pending idle tasks
def update_data(self):
if not self.var1.get(): self.var1.set('0')
iteration = int(self.var1.get())
print('iteration', iteration)
if iteration < 3:
iteration = iteration + 1
self.var1.set(str(iteration))
self.update_idletasks() # Calls all pending idle tasks
time.sleep(1)
self.update_data()
main = GUI()
main.mainloop()
You will have to research after() yourself as I can't help you there.
I am stuck trying to dynamically display a specific image on a tk page based on button clicked on a previous page. PageOne has 5 images with 5 buttons below each. Clicking on specific button should take the user to the second page and display image 3 if the 3rd button is clicked.
I have figured out how to pass a value of 1 to 5 depending on which button is clicked, and the images are saved pic1.gif,...pic5.gif so to return the correct image I just need to append the value to the file location.
I am struggling to figure out how to refresh PageTwo when it is accessed.
Any help would be greatly appreciated, thanks.
TITLE_FONT = ("Helvetica", 18, "bold")
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
self.attributes("-fullscreen", False)
self.geometry('{}x{}'.format(1000, 1000))
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(PageOne)
def show_frame(self, c):
frame = self.frames[c]
frame.tkraise()
class PageOne(tk.Frame):
praiseid = 0
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
def PraiseClick(button_id):
PageOne.praiseid = button_id
controller.show_frame(PageTwo)
users= [1,2,3,4,5]
for i in users:
location = str('C:/Users/XXXXXXX/Documents/pic'+str(i)+'.gif')
icon = tk.PhotoImage(file=location)
IDlabel = tk.Label(self,image=icon)
IDlabel.image = icon
IDlabel.place(x=i*100,y=200,width=100,height=100)
for j in users:
praisebutton = tk.Button(self,text="Click",width=10,command=lambda x=j: PraiseClick(int(x)))
praisebutton.place(x=j*100,y=300,width=100,height=44)
backbutton = tk.Button(self, text="Go to Start Page",
command=lambda: controller.show_frame(StartPage))
backbutton.place(x=100,y=50,width=200,height=44)
class PageTwo(tk.Frame):
def get_id(self):
return(PageOne.praiseid)
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.location = 'C:/Users/XXXXXXX/Documents/pic'+str(self.get_id())+'.gif'
icon = tk.PhotoImage(file=self.location)
self.IDlabel = tk.Label(self,image=icon)
self.IDlabel.image = icon
self.IDlabel.place(x=0,y=200,width=100,height=100)
The selected image from the PageOne is not drawn on the PageTwo because the drawing function is localized in the __init__() function and no event is raise when the PageTwo is redraw by calling the function tkraise().
Problem 1 - generate an event when calling tkraise() of the PageTwo.
Here, the OOP of Python will be the answer by overriding the function
tkraise() in the class PageTwo.
class PageTwo(tk.Frame):
...
def tkraise(self):
print('PageTwo.tkraise()')
tk.Frame.tkraise(self)
# then call the drawing icon
self.refresh_icon() # see Problem 2
Problem 2 - localize the drawing of the icon in a function of the class PageTwo.
To take into account of the new selected icon, create a function
refresh_icon() in the class PageTwo and call it from both
__init__() and tkraise() functions.
class PageTwo(tk.Frame):
...
def refresh_icon(self):
self.location = 'C:/Users/XXXXXXX/Documents/pic'+str(self.get_id())+'.gif'
icon = tk.PhotoImage(file=self.location)
self.IDlabel = tk.Label(self,image=icon)
self.IDlabel.image = icon
self.IDlabel.place(x=0,y=200,width=100,height=100)
Add at the end of the __init__() function.
class PageTwo(tk.Frame):
...
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
...
self.refresh_icon()
Bonus 1 - to prevent an Exception in case of missing image, add a check before.
Create a file_exists() function, then check before loading in
PhotoImage().
def file_exists(filepath):
try:
fp_file = open(filepath)
return (True)
except IOError:
return (False)
And in the function refresh_icon() of the class PageTwo:
self.location = 'C:/Users/XXXXXXX/Documents/pic'+str(self.get_id())+'.gif'
if (file_exists(self.location)):
icon = tk.PhotoImage(file=self.location)
...
Bonus 2 - clear the current IDlabel in case of image not loaded.
when creating a new Label in the PageTwo, the variable
self.IDlabel will store the new Image without deleting the old one.
Before creating a new one, call the destroy() function.
Add a declaration of the variable self.IDlabel and assign it to None in the __init__() function. Then call destroy() in the
class PageTwo(tk.Frame):
...
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
...
self.refresh_icon()
self.IDlabel = None
...
def refresh_icon(self):
self.location = 'C:/Users/XXXXXXX/Documents/pic'+str(self.get_id())+'.gif'
if (self.IDlabel): # check label and destroy it
self.IDlabel.destroy()
if (file_exists(self.location)):
...