Combine two window to one window in tkinter - python

I am trying to combine two window into one window in Tkinter. I tried in many ways, I find on the web. My requirement is: if I press a button then it should create a blank screen.
Here is the code:
1. main.py
from tkinter import *
from PIL import ImageTk
import PIL.Image
from employee import employeeClass
class IMS:
def __init__(self,root):
self.root=root
self.root.geometry("1350x700+0+0")
self.root.title("IMS | Developed by Amit")
self.root.config(bg="white")
photo = PhotoImage(file = "images\i1.png")
self.root.iconphoto(False, photo)
if __name__ =="__main__":
os.system('python start.py')
root = Tk()
obj = IMS(root)
root.mainloop()
1. main2.py
from tkinter import *
from PIL import ImageTk
import PIL.Image
class employeeClass:
def __init__(self,root):
self.root=root
self.root.geometry("1055x480+205+127")
self.root.title("IMS | Developed by Amit")
self.root.config(bg="white")
photo = PhotoImage(file = "images\i1.png")
self.root.iconphoto(False, photo)
self.root.focus_force()
self.root.resizable(False, False)
#root.overrideredirect(1)
#-----Search-----
SearchFrame=LabelFrame(self.root, text="Search Employee", bg="white")
SearchFrame.place(x=250, y=20,width=600,height=70)
if __name__ =="__main__":
root = Tk()
obj = employeeClass(root)
root.mainloop()

Related

How to change whole app's theme when used within Class, Tkinter, ttkThemes, ThemedTk Python

Summary:
I searched the internet about additional themes for tkinter. I found ttkthemes - ThemedTk option. However, my app is created within class. The difference between the example shown in the module website, the themedtk example is applied with root method. Here is the code:
import os
from tkinter import *
import tkinter as tk
from tkinter import ttk, filedialog
from PIL import ImageTk, Image
from ttkthemes import ThemedTk,THEMES
class App(tk.Tk):
def __init__(self):
super().__init__()
self.style = ThemedTk
self.title(dil_sec[0])
self.geometry("800x600")
self.my_notebook = ttk.Notebook(self)
self.my_notebook.pack(pady=15)
self.my_menu = Menu(self)
self.config(menu=self.my_menu)
rec = None
id_num = None
self.first_lane = Menu(self.my_menu)
self.my_menu.add_cascade(label=dil_sec[6], menu=self.first_lane)
self.first_lane.add_command(label=dil_sec[2], command=self.customer_list)
self.first_lane.add_command(label=dil_sec[1], command=lambda: self.customer_add(rec, id_num))
self.second_lane = Menu(self.my_menu)
self.my_menu.add_cascade(label=dil_sec[3], menu=self.second_lane)
self.third_lane = Menu(self.my_menu)
self.my_menu.add_cascade(label= dil_sec[65], menu=self.third_lane)
self.third_lane.add_command(label=THEMES[0],command=lambda: self.stil_changer(still=THEMES[0]))
self.third_lane.add_command(label=THEMES[1],command=lambda: self.stil_changer(still=THEMES[1]))
def stil_changer(self,still):
print(still)
self.style.set_theme(self,theme_name=still)
if __name__ == "__main__":
app = App()
app.mainloop()
When I run the application and the click the style and choose a style. I receive this error:
AttributeError: '_tkinter.tkapp' object has no attribute '_toplevel'
It took so much time of me to solve it. Thanks in advance.
You forgot () in line
self.style = ThemedTk()
and later you have to remove self in
self.style.set_theme(theme_name=still)
and now it runs without error but still it doesn't change theme.
Maybe it needs to use widgets from ttk.
EDIT:
You have to use ThemedTk in place of tk.Tk. And use directly self.set_theme()
And of course you need some widgets from ttk.
import tkinter as tk
import tkinter.ttk as ttk
from ttkthemes import ThemedTk
class App(ThemedTk):
def __init__(self):
super().__init__()
self.geometry("800x600")
self.my_notebook = ttk.Notebook(self)
self.my_notebook.pack(pady=15, fill='both', expand=True)
self.frame = ttk.Frame(self.my_notebook)
self.my_notebook.add(self.frame, text='Buttons')
for number in range(10):
b = ttk.Button(self.frame, text=str(number))
b.pack()
self.my_menu = tk.Menu(self)
self.config(menu=self.my_menu)
self.first_lane = tk.Menu(self.my_menu)
self.first_lane.add_command(label='a')
self.first_lane.add_command(label='b')
self.my_menu.add_cascade(label='Menu1', menu=self.first_lane)
self.second_lane = tk.Menu(self.my_menu)
self.my_menu.add_cascade(label='Menu2', menu=self.second_lane)
self.third_lane = tk.Menu(self.my_menu)
self.my_menu.add_cascade(label='Style', menu=self.third_lane)
for item in sorted(self.get_themes()):
self.third_lane.add_command(label=item, command=lambda name=item: self.changer_theme(name))
def changer_theme(self, name):
print('theme:', name)
self.set_theme(name)
if __name__ == "__main__":
app = App()
app.mainloop()
Or you can use Tk with ThemedStyle (instead of ThemedTk)
and then you can set self.style = ThemedStyle()
but you have to use self.style.theme_use(name) instead of self.set_theme(name)
And it needs self.style.get_themes() instead of self.get_themes().
import tkinter as tk
import tkinter.ttk as ttk
from ttkthemes import ThemedStyle
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry("800x600")
self.style = ThemedStyle() # with Tk and ThemedStyle
self.my_notebook = ttk.Notebook(self)
self.my_notebook.pack(pady=15, fill='both', expand=True)
self.frame = ttk.Frame(self.my_notebook)
self.my_notebook.add(self.frame, text='Buttons')
for number in range(10):
b = ttk.Button(self.frame, text=str(number))
b.pack()
self.my_menu = tk.Menu(self)
self.config(menu=self.my_menu)
self.first_lane = tk.Menu(self.my_menu)
self.first_lane.add_command(label='a')
self.first_lane.add_command(label='b')
self.my_menu.add_cascade(label='Menu1', menu=self.first_lane)
self.second_lane = tk.Menu(self.my_menu)
self.my_menu.add_cascade(label='Menu2', menu=self.second_lane)
self.third_lane = tk.Menu(self.my_menu)
self.my_menu.add_cascade(label='Style', menu=self.third_lane)
#for item in sorted(self.get_themes()): # with ThemedTk
for item in sorted(self.style.get_themes()): # with Tk and ThemedStyle
self.third_lane.add_command(label=item, command=lambda name=item: self.changer_theme(name))
def changer_theme(self, name):
print('theme:', name)
#self.set_theme(name) # with ThemedTk
self.style.theme_use(name) # with Tk and ThemedStyle
if __name__ == "__main__":
app = App()
app.mainloop()
I found screenshots for all themes: List of ttk Themes
OLD ANSWER
I have only working example with ttk.Buttons
import tkinter as tk
from tkinter import ttk
import ttkthemes
root = tk.Tk()
root.style = ttkthemes.ThemedStyle()
for i, name in enumerate(sorted(root.style.theme_names())):
b = ttk.Button(root, text=name, command=lambda name=name:root.style.theme_use(name))
b.pack(fill='x')
root.mainloop()
Default:
Blue:
Kroc:
Radiance or Ubuntu:
Winxpblue:
Source keep on GitHub: furas / python-examples / tkinter / themes-change-ttkthemes / buttons

How to open another GUI window, once video is completed in GUI in python tkinter?

The purpose of the below code is to automatically open another window of GUI once video is completed (the length of video is 10 seconds)
I have tried to do this but some how its not working pl. have a look at below code.
import tkinter as tk, threading
import imageio
import tkinter as tk
from tkinter import *
from tkinter import ttk
import tkinter.font as font
import os
import re
from PIL import Image, ImageTk
global Image
global image
global Label
video_name = "C:\\Users\\Ray\\Desktop\\Duplicate.mp4"
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
if __name__ == "__main__":
root = tk.Tk()
my_label = tk.Label(root)
my_label.pack()
thread = threading.Thread(target=stream, args=(my_label,))
thread.daemon = 1
thread.start()
ima = Image.open('C:\\Users\\Ray\\Desktop\\July_bill.jpg')
ima = ima.resize((1920,1050), Image.ANTIALIAS)
my_img = ImageTk.PhotoImage(ima)
my_lbl = Label(Image = my_img)
my_lbl.pack()
root.mainloop()
In this code if I remove the image inserting code ( which is ima, my_img lines in code) the video plays smoothly but if i use this code then it does not works it shows the error of "unknown option "-Image"
Can anybody help me in getting this solved it would be great.
Regards,
Ray
Just change my_lbl = Label(Image = my_img) to my_lbl = Label(image = my_img)
You can see TK Label options here. As you can see "image" starts with lowercase, in your code, you had "Image" that caused the error.
Full code:
import tkinter as tk, threading
import imageio
from tkinter import *
from PIL import Image, ImageTk
from time import sleep
def stream(label):
video_name = "C:\\Users\\Ray\\Desktop\\Duplicate.mp4"
video = imageio.get_reader(video_name)
for image in video.iter_data():
frame_image = ImageTk.PhotoImage(Image.fromarray(image))
label.config(image=frame_image)
label.image = frame_image
if __name__ == "__main__":
root = tk.Tk()
my_label = tk.Label(root)
my_label.pack()
thread = threading.Thread(target=stream, args=(my_label,))
thread.daemon = 1
thread.start()
sleep(10)
ima = Image.open('C:\\Users\\Ray\\Desktop\\July_bill.jpg')
ima = ima.resize((1920,1050), Image.ANTIALIAS)
my_img = ImageTk.PhotoImage(ima)
my_lbl = Label(image = my_img)
my_lbl.pack()
root.mainloop()
I have not tested the video, but the image works well.

How to create an image button in tkinter? and how to put into another interface?

from tkinter import *
from PIL import ImageTk, Image
root = Tk()
def save():
editor = Tk()
img= Image.open('images/save.png')
fixed= ImageTk.PhotoImage(img)
btn_save_editor = Button(editor, image=fixed)
btn_save_editor.place(x=30, y=80, height=20)
root.mainloop()
From another interface, if you mean another window, then you should use a TopLevel widget, not TK
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
def save():
editor = Toplevel()
editor.geometry("500x500") # you can change it
img = Image.open('images/save.png')
fixed= ImageTk.PhotoImage(img)
btn_save_editor = Button(editor, image=fixed)
btn_save_editor.place(x=30, y=80, height=20)
save()
root.mainloop()

Tkinter Image icon not working or aligning properly in a Labelframe

This question concerns Python's Tkinter.
I first produced this GUI, a simple two-column set of rows in a Labelframe, with an icon on the right:
The above behaviour was correct and expected, based on this following code:
import tkinter as tk
import tkinter.ttk as ttk
from PIL import Image, ImageTk
root = tk.Tk()
icon_colours_fp = r"D:\Dropbox\coding\python\experiments\icon_component.gif"
icon_col = tk.PhotoImage(file=icon_colours_fp)
# icon_col = ImageTk.PhotoImage(Image.open(icon_colours_fp))
tk.Label(root, text="Past").grid(row=0, column=0)
tk.Label(root, text="Today").grid(row=1, column=0)
tk.Label(root, text="Future").grid(row=2, column=0)
_b = ttk.Button(root, image=icon_col)
_b['image'] =icon_col
_b.grid(row=0, column=1)
root.mainloop()
I then re-wrote the code as a class, hoping to produce something similar within a Labelframe:
import tkinter as tk
import tkinter.ttk as ttk
from PIL import Image, ImageTk
class Options(tk.Frame):
def __init__(self, parent):
super().__init__()
main_labelframe = ttk.LabelFrame(parent, text="Test Labelframe")
main_labelframe.pack(fill=tk.BOTH, expand=1)
frame_1 = tk.Frame(main_labelframe)
frame_1_sep = ttk.Separator(main_labelframe, orient=tk.VERTICAL)
frame_2 = tk.Frame(main_labelframe)
frame_1.pack(side=tk.LEFT)
frame_1_sep.pack(side=tk.LEFT, fill=tk.BOTH)
frame_2.pack(side=tk.LEFT)
tk.Label(frame_1, text="Past").grid(row=0, column=0)
tk.Label(frame_1, text="Today").grid(row=1)
tk.Label(frame_1, text="Future").grid(row=2)
icon_colours_fp = r"D:\Dropbox\coding\python\experiments\icon_component.gif"
icon_col = tk.PhotoImage(file=icon_colours_fp)
_b = ttk.Button(frame_2, image=icon_col)
_b['image'] = icon_col
_b.grid(row=0, column=0)
class Gui(tk.Tk):
def __init__(self):
super().__init__()
options = Options(self)
options.pack()
gui = Gui()
gui.mainloop()
The code then failed, in two respects:
The icon fails to appear.
The ttk Button becomes misaligned. (It appears in the centre, whereas by the grid, it should appear at the top.)
The failed code appears as follows:
I have experimented: among others, I changed the geometry manager to .pack(), and changed the parent of ttk.Button, but without success. Would appreciate some pointers as to where I've gone wrong, especially as to the disappearing icon.
You didn't keep a reference to the image. Easiest way here is to change:
icon_col = tk.PhotoImage(file=icon_colours_fp)
b = ttk.Button(frame_2, image=icon_col)
_b['image'] = icon_col
To:
self.icon_col = tk.PhotoImage(file=icon_colours_fp)
b = ttk.Button(frame_2, image=self.icon_col)

How to update the image of a Tkinter Label widget?

I would like to be able to swap out an image on a Tkinter label, but I'm not sure how to do it, except for replacing the widget itself.
Currently, I can display an image like so:
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
However, when the user hits, say the ENTER key, I'd like to change the image.
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
def callback(e):
# change image
root.bind("<Return>", callback)
root.mainloop()
Is this possible?
The method label.configure does work in panel.configure(image=img).
What I forgot to do was include the panel.image=img, to prevent garbage collection from deleting the image.
The following is the new version:
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
def callback(e):
img2 = ImageTk.PhotoImage(Image.open(path2))
panel.configure(image=img2)
panel.image = img2
root.bind("<Return>", callback)
root.mainloop()
The original code works because the image is stored in the global variable img.
Another option to do it.
Using object-oriented programming and with an interactive interface to update the image.
from Tkinter import *
import tkFileDialog
from tkFileDialog import askdirectory
from PIL import Image
class GUI(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
w,h = 650, 650
master.minsize(width=w, height=h)
master.maxsize(width=w, height=h)
self.pack()
self.file = Button(self, text='Browse', command=self.choose)
self.choose = Label(self, text="Choose file").pack()
self.image = PhotoImage(file='cualitativa.gif')
self.label = Label(image=self.image)
self.file.pack()
self.label.pack()
def choose(self):
ifile = tkFileDialog.askopenfile(parent=self,mode='rb',title='Choose a file')
path = ifile.name
self.image2 = PhotoImage(file=path)
self.label.configure(image=self.image2)
self.label.image=self.image2
root = Tk()
app = GUI(master=root)
app.mainloop()
root.destroy()
Replace 'cualitativa.jpg' for the default image you want to use.
Another solution that might be of help.
In my case, I had two tk.Tk() windows. When using ImageTk.PhotoImage(), the object defaults to setting its tk window to being the first one created. A simple fix to this is to pass the tk window that you want as such ImageTk.PhotoImage(img, master=your_window)
import tkinter as tk
from PIL import ImageTk, Image
if __name__ == '__main__':
main_window = tk.Tk()
second_window = tk.Tk()
main_canvas = Canvas(second_window)
main_canvas.pack()
filename = 'test.png'
img = Image.open(filename)
img = img.resize((300, 100), Image.ANTIALIAS)
logo = ImageTk.PhotoImage(img, master=second_window)
logo_label = Label(master=main_canvas, image=logo)
logo_label.image = logo
logo_label.pack()
main_window.mainloop()

Categories