TKinter Scrollbar - Problem with too many scrollbars - python-3.x

I am building a simple UI using Tkinter and running into a problem when adding scrollbars. The layout of my UI (as shown below) consists of 5 frames, with one frame having 3 sub-frames. I am trying to add scrollbars to each frame and the sub-frames, however when I add the last scroll-bar to the sub-frame, all of the frames become smaller than their designated size. This only occurs when the final scrollbar is added.
Code:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("900x600")
notebook = ttk.Notebook(root)
frame1 = tk.Frame(root, width = 900, height = 600)
frame2 = tk.Frame(root, width = 900, height = 600)
frame3 = tk.Frame(root, width = 900, height = 600)
frame4 = tk.Frame(root, width = 900, height = 600)
frame5 = tk.Frame(root, width = 900, height = 600)
notebook.add(frame1, text = 'Frame 1')
notebook.add(frame2, text = 'Frame 2')
notebook.add(frame3, text = 'Frame 3')
notebook.add(frame4, text = 'Frame 4')
notebook.add(frame5, text = 'Frame 5')
canvas2 = tk.Canvas(frame2, width = 900, height = 600, scrollregion = (0, 0, 900, 600))
canvas3 = tk.Canvas(frame3, width = 900, height = 600, scrollregion = (0, 0, 900, 600))
canvas4 = tk.Canvas(frame4, width = 900, height = 600, scrollregion = (0, 0, 900, 600))
canvas5 = tk.Canvas(frame5, width = 900, height = 600, scrollregion = (0, 0, 900, 600))
canvas2.pack(side = tk.LEFT, fill = tk.BOTH, expand = 1)
canvas3.pack(side = tk.LEFT, fill = tk.BOTH, expand = 1)
canvas4.pack(side = tk.LEFT, fill = tk.BOTH, expand = 1)
canvas5.pack(side = tk.LEFT, fill = tk.BOTH, expand = 1)
et_vbar2 =tk.Scrollbar(canvas2, orient = tk.VERTICAL)
et_vbar2.pack(side = tk.RIGHT, fill = tk.Y)
et_vbar2.config(command=canvas2.yview)
canvas2.config(yscrollcommand = et_vbar2.set)
et_hbar2 =tk.Scrollbar(canvas2, orient = tk.HORIZONTAL)
et_hbar2.pack(side = tk.BOTTOM, fill = tk.X)
et_hbar2.config(command=canvas2.xview)
canvas2.config(xscrollcommand = et_hbar2.set)
et_vbar3 =tk.Scrollbar(canvas3, orient = tk.VERTICAL)
et_vbar3.pack(side = tk.RIGHT, fill = tk.Y)
et_vbar3.config(command=canvas3.yview)
canvas3.config(yscrollcommand = et_vbar3.set)
et_hbar3 =tk.Scrollbar(canvas3, orient = tk.HORIZONTAL)
et_hbar3.pack(side = tk.BOTTOM, fill = tk.X)
et_hbar3.config(command=canvas3.xview)
canvas3.config(xscrollcommand = et_hbar3.set)
et_vbar4 =tk.Scrollbar(canvas4, orient = tk.VERTICAL)
et_vbar4.pack(side = tk.RIGHT, fill = tk.Y)
et_vbar4.config(command=canvas4.yview)
canvas4.config(yscrollcommand = et_vbar4.set)
et_hbar4 =tk.Scrollbar(canvas4, orient = tk.HORIZONTAL)
et_hbar4.pack(side = tk.BOTTOM, fill = tk.X)
et_hbar4.config(command=canvas4.xview)
canvas4.config(xscrollcommand = et_hbar4.set)
et_vbar5 =tk.Scrollbar(canvas5, orient = tk.VERTICAL)
et_vbar5.pack(side = tk.RIGHT, fill = tk.Y)
et_vbar5.config(command=canvas5.yview)
canvas5.config(yscrollcommand = et_vbar5.set)
et_hbar5 =tk.Scrollbar(canvas5, orient = tk.HORIZONTAL)
et_hbar5.pack(side = tk.BOTTOM, fill = tk.X)
et_hbar5.config(command=canvas5.xview)
canvas5.config(xscrollcommand = et_hbar5.set)
notebook.pack()
notebook2 = ttk.Notebook(frame1)
tab1 = tk.Frame(frame1, width = 900, height = 600)
tab2 = tk.Frame(frame1, width = 900, height = 600)
tab3 = tk.Frame(frame1, width = 900, height = 600)
notebook2.add(tab1, text= 'Tab 1')
notebook2.add(tab2, text = 'Tab 2')
notebook2.add(tab3, text = 'Tab 3')
subcanvas1 = tk.Canvas(tab1, width = 900, height = 600, scrollregion = (0, 0, 900, 600))
subcanvas2 = tk.Canvas(tab2, width = 900, height = 600, scrollregion = (0, 0, 900, 600))
subcanvas3 = tk.Canvas(tab3, width = 900, height = 600, scrollregion = (0, 0, 900, 600))
subcanvas1.pack(side = tk.LEFT, fill = tk.BOTH, expand = 1)
subcanvas2.pack(side = tk.LEFT, fill = tk.BOTH, expand = 1)
subcanvas3.pack(side = tk.LEFT, fill = tk.BOTH, expand = 1)
subet_vbar1 =tk.Scrollbar(subcanvas1, orient = tk.VERTICAL)
subet_vbar1.pack(side = tk.RIGHT, fill = tk.Y)
subet_vbar1.config(command=subcanvas1.yview)
subcanvas1.config(yscrollcommand = subet_vbar1.set)
subet_hbar1 =tk.Scrollbar(subcanvas1, orient = tk.HORIZONTAL)
subet_hbar1.pack(side = tk.BOTTOM, fill = tk.X)
subet_hbar1.config(command=subcanvas1.xview)
subcanvas1.config(xscrollcommand = subet_hbar1.set)
subet_vbar2 =tk.Scrollbar(subcanvas2, orient = tk.VERTICAL)
subet_vbar2.pack(side = tk.RIGHT, fill = tk.Y)
subet_vbar2.config(command=subcanvas2.yview)
subcanvas2.config(yscrollcommand = subet_vbar2.set)
subet_hbar2 =tk.Scrollbar(subcanvas2, orient = tk.HORIZONTAL)
subet_hbar2.pack(side = tk.BOTTOM, fill = tk.X)
subet_hbar2.config(command=subcanvas2.xview)
subcanvas2.config(xscrollcommand = subet_hbar2.set)
subet_vbar3 =tk.Scrollbar(subcanvas3, orient = tk.VERTICAL)
subet_vbar3.pack(side = tk.RIGHT, fill = tk.Y)
subet_vbar3.config(command=subcanvas3.yview)
subcanvas3.config(yscrollcommand = subet_vbar3.set)
subet_hbar3 =tk.Scrollbar(subcanvas3, orient = tk.HORIZONTAL)
subet_hbar3.pack(side = tk.BOTTOM, fill = tk.X)
subet_hbar3.config(command=subcanvas3.xview)
subcanvas3.config(xscrollcommand = subet_hbar3.set)
notebook2.pack()
root.mainloop()
Is there a way to prevent every frame from becoming smaller after adding the scrollbars?

There's a number of problems in your code. Rather than try to fix them, I think you should cheat and use the code from someone who has abstracted this. There are many people who have made a scrolledframe in some form, including me. To use mine, download the DoubleScrolledFrame.py file from here and then:
import tkinter as tk
from tkinter import ttk
from DoubleScrolledFrame import DoubleScrolledFrame
def add_scrolled_frame(notebook, text):
"""do this in a function so that you can easily change parameters"""
frame = DoubleScrolledFrame(notebook, width = 900, height = 600)
notebook.add(frame, text = text)
return frame
root = tk.Tk()
notebook = ttk.Notebook(root)
frame1 = tk.Frame(root)
notebook.add(frame1, text = 'Frame 1')
frame2 = add_scrolled_frame(notebook, text = 'Frame 2')
frame3 = add_scrolled_frame(notebook, text = 'Frame 3')
frame4 = add_scrolled_frame(notebook, text = 'Frame 4')
frame5 = add_scrolled_frame(notebook, text = 'Frame 5')
notebook.pack()
notebook2 = ttk.Notebook(frame1)
tab1 = add_scrolled_frame(notebook2, text = 'Tab 1')
tab2 = add_scrolled_frame(notebook2, text = 'Tab 2')
tab3 = add_scrolled_frame(notebook2, text = 'Tab 3')
notebook2.pack()
# assuming you want widgets in these frames:
for i in range(30):
lbl = tk.Label(tab1, text=f'This is label number {i}')
lbl.pack()
root.mainloop()

Related

PhotoImage Tkinter problem: Button not working and not displaying

im using Proxlight Designer to create Drag-n-drop GUI. It is a application that works with open-cv-python, but a Button is not displaying. It seems as if open-cv is the problem, because if you remove it the Button displays and works properly. Here is the code for the GUI:
cap = cv2.VideoCapture(0)
window = Tk()
window.geometry("700x800")
window.configure(bg = "#ffffff")
canvas = Canvas(
window,
bg = "#ffffff",
height = 800,
width = 700,
bd = 0,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
l1 = Label(bg = "black")
l1.place(x = 100, y = 150, width = 500, height = 500)
img0 = PhotoImage(file = f"RES/img1.png")
b0 = Button(
image = img0,
borderwidth = 0,
highlightthickness = 0,
command = save_face,
relief = "flat")
b0.place(
x = 250, y = 693,
width = 200,
height = 75)
img1 = PhotoImage(file = f"RES/img2.png")
b1 = Button(
image = img1,
borderwidth = 0,
highlightthickness = 0,
command = encryptPass,
relief = "flat")
b1.place(
x = 480, y = 693,
width = 200,
height = 75)
img2 = PhotoImage(file = f"RES/img3.png")
b2 = Button(
image = img2,
borderwidth = 0,
highlightthickness = 0,
command = generate_key,
relief = "flat")
b2.place(
x = 20, y = 693,
width = 200,
height = 75)
window.resizable(False, False)
while True:
img = cap.read()[1]
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = ImageTk.PhotoImage(Image.fromarray(img1))
l1["image"] = img
window.update()
So thanks to the comments of #Matiiss and #acw1668 (thx alot btw) i got it to work. Basically the while loop was the problem. I fixed it using this instead of the while loop:
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
def show_frame():
_, frame = cap.read()
global cv2image
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
l1.imgtk = imgtk
l1.configure(image=imgtk)
l1.after(10, show_frame)
window = Tk()
window.geometry("700x800")
window.configure(bg = "#ffffff")
l1 = Label(bg = "black")
l1.place(x = 100, y = 150, width = 500, height = 500)
window.resizable(False, False)
show_frame()
window.mainloop()

How can I scroll multiple frames in canvas?

I want to create a list of frames with further features like a button, label e.g.. but my issues are the size of the LabelFrame. If I put the LabelFrame in container it fits like I want to but it isn't scrollable any more. Any ideas?
from tkinter import ttk
import tkinter as tk
root = tk.Tk()
container = ttk.Frame(root)
canvas = tk.Canvas(container)
scrollbar = ttk.Scrollbar(container, orient="vertical", command=canvas.yview)
scrollable_frame = ttk.Frame(canvas)
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
for i in range(50):
lf = ttk.Frame(scrollable_frame, text=i).grid(column=1, row=i)
frame_ip = tk.LabelFrame(lf, bg="white", text=i)
frame_ip.place(relwidth=0.95, relheight=0.2, relx=0.025, rely=0)
button_scroll1 = tk.Button(frame_ip, text="Start", bg="grey")
button_scroll1.place(relwidth=0.15, relx=0.025, relheight=0.15, rely=0.1)
container.pack()
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
root.mainloop()
Here is your updated code with a Button, Canvas and Scrollbar inserted into each LabelFrame with grid manager.
I've also made the container resizable with row|column configure.
Seems to work fine.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.rowconfigure(0, weight = 1)
root.columnconfigure(0, weight = 1)
root.geometry("341x448")
container = ttk.Frame(root)
container.rowconfigure(0, weight = 1)
container.columnconfigure(0, weight = 1)
canvas = tk.Canvas(container)
scrollbar = ttk.Scrollbar(container, orient = tk.VERTICAL, command = canvas.yview)
scrollable_frame = ttk.Frame(canvas)
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")))
canvas.create_window((0, 0), window = scrollable_frame, anchor = tk.NW)
canvas.configure(yscrollcommand = scrollbar.set)
for i in range(15):
L = ttk.LabelFrame(scrollable_frame, text = "Sample scrolling label")
L.grid(row = i, column = 0, sticky = tk.NSEW)
B = ttk.Button( L, text = f"Button {i}")
B.grid(row = 0, column = 0, sticky = tk.NW)
K = tk.Canvas(
L, width = 300, height = 100,
scrollregion = "0 0 400 400", background = "#ffffff")
K.grid(row = 1, column = 0, sticky = tk.NSEW)
S = ttk.Scrollbar( L, command = K.yview)
S.grid(column = 1, row = 1, sticky = tk.NSEW)
K["yscrollcommand"] = S.set
container.grid(row = 0, column = 0, sticky = tk.NSEW)
canvas.grid(row = 0, column = 0, sticky = tk.NSEW)
scrollbar.grid(row = 0, column = 1, sticky = tk.NS)
root.mainloop()

Scrollbar not showing - tkinter

I have read threads on site to try to resolve this issue but nothing was found that I have not tried where it still does not work.
I am having a little trouble getting the scrollbar to show for my widgets. I have 2 scrollbars that have been written the same way for a Listbox and Text widget in another window and show and function perfectly but in this window the 2 scrollbars written will not display.
I have the code the same as the other functioning scrollbars in the other window but for some reason these 2 are not displaying.
They are placed at ingred and instruc
Some advice would be appreciated.
Code:
import os
import tkinter
from tkinter import *
window_3 = tkinter.Toplevel()
window_3.title('Recipes')
window_3.wm_iconbitmap('recipe.ico')
w = 1024
h = 612
ws = window_3.winfo_screenwidth()
hs = window_3.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
window_3.geometry('%dx%d+%d+%d' % (w, h, x, y))
window_3.title('Recipes')
Recipe_Selection = StringVar()
Dish_Type = StringVar()
Recipe = StringVar()
Cook_Time = StringVar()
Serves = StringVar()
ingred = StringVar()
instruct = StringVar()
dish_type = tkinter.Label(window_3, font=('Times 9 bold'), text='Category:', bg = "#FFD599", fg = '#9A0615')
dish_type.pack()
dish_type.place(x=210, y=125)
dish_type_entry = tkinter.Entry(window_3, textvariable=Dish_Type, width = 29, bg = "#FFD599", fg = '#9A0615', justify=CENTER)
dish_type_entry.place(x=275, y=126) recipe = tkinter.Label(window_3, font=('Times 9 bold'), text='Recipe:', bg = "#FFD599", fg = '#9A0615')
recipe.pack()
recipe.place(x=210, y=145)
recipe_entry = tkinter.Entry(window_3, textvariable=Recipe, width = 29, bg = "#FFD599", fg = '#9A0615', justify=CENTER)
recipe_entry.place(x=275, y=146)
serves = tkinter.Label(window_3, font=('Times 9 bold'), text='Serves:', bg = "#FFD599", fg = '#9A0615')
serves.pack()
serves.place(x=547, y=125)
serves_entry = tkinter.Entry(window_3, textvariable=Serves, width = 3, bg = "#FFD599", fg = '#9A0615', justify=CENTER)
serves_entry.place(x=623, y=126)
cook_time = tkinter.Label(window_3, font=('Times 9 bold'), text='Cook Time:', bg = "#FFD599", fg = '#9A0615')
cook_time.pack()
cook_time.place(x=547, y=145)
cook_time_entry = tkinter.Entry(window_3, textvariable=Cook_Time, width = 11, bg = "#FFD599", fg = '#9A0615', justify=CENTER)
cook_time_entry.place(x=623, y=146)
ingred = tkinter.Text(window_3, font=('Times 9'), height = 20, width=40, bd=1, bg = "#FFD599", fg = '#9A0615')
ingred.pack()
ingred.place(x=210, y=200)
yscroll = tkinter.Scrollbar(command=ingred.yview, orient=tkinter.VERTICAL)
yscroll.place(x=452, y=200)
ingred.configure(yscrollcommand=yscroll.set)
instruct = tkinter.Text(window_3, font=('Times 9'), height = 20, width=40, bd=1, bg = "#FFD599", fg = '#9A0615')
instruct.pack()
instruct.place(x=547, y=200)
yscroll = tkinter.Scrollbar(command=instruct.yview, orient=tkinter.VERTICAL)
yscroll.place(x=789, y=200)
instruct.configure(yscrollcommand=yscroll.set)
dish_type_entry.focus()
saveButton = tkinter.Button(window_3, text='Save Recipe', font='Times 9 bold italic', border = 1, height = 1, width = 14, bg = "#F9F8D6", fg = '#9A0615')##, command = Save)
saveButton.pack
saveButton.place(x=293, y=527)
clear1Button = tkinter.Button(window_3, text='Clear Page', font='Times 9 bold italic', border = 1, height = 1, width = 14, bg = "#F9F8D6", fg = '#9A0615')##, command = clear2)
clear1Button.pack
clear1Button.place(x=630, y=527)
backButton = tkinter.Button(window_3, text='Back', font='Times 9 bold italic', border = 1, height = 1, width = 14, bg = "#F9F8D6", fg = '#9A0615')##, command = close3)
backButton.pack
backButton.place(x=449, y=527)
window_3.mainloop()
You forgot to tell Scrollbar() about its master, so it naturally assumes it's root window. Do:
yscroll = Scrollbar(window_3, ... etc.
By the way, both ingred and instruct are associated with the same scrollbar: yscroll but I gather its just from cutting and pasting code. Also you define StringVar() instances with the same name as the text widgets.

Python 2 tkinter, a loan window appears when it has not been called for

I have come across an issue with using the tkinter message box, when a user of my application presses the English flag and then goes to Log-out in the top right hand corner a message box appears but also another box appears with the window title 'tk', I have not made or called any such window so my theory is that the window is coming from some place with the message box or maybe its something I have mistakenly done in my code which I cant see.
here is my code below;
import tkinter
from tkinter import *
from tkinter import messagebox as box
Title_Text = 25
Title_Font = "Courier"
Font = 'Ariel'
Rest_Text = 16
Background_Colour = 'Light blue'
def EngFlag():
print('Hello world')
def LogOut1():
response = box.askquestion ('?', 'Are You Sure?')
if response == 'yes':
try:
window.destroy()
except:
Eng_window.destroy()
def back1():
Home_Screen()
def Home_Screen():
global window
window = Tk()
window.geometry('1366x768')
window.configure(background = Background_Colour)
window.title('Local Languages Learning System')
window.resizable(width=False, height=False)
try:
Eng_window.destroy()
except:
pass
Title1 = Label(window, text = 'Local Languages Home Screen', bg = Background_Colour)
Title1.config(font=("Courier", Title_Text))
Title1.pack()
Title1.place(y = 1, x = 450)
Question_Label = Label(window, text = 'Please Pick The Language You Wish To Learn', bg = Background_Colour)
Question_Label.config(font=(Font, Rest_Text))
Question_Label.pack()
Question_Label.place(y = 200, x = 495)
Log_Out = Button(window, text = 'Log-Out', height = 1, width = 8, command = LogOut1)
Log_Out.pack()
Log_Out.place(y = 5, x = 1290)
help_btn = Button(window, text = 'Help', height = 1, width = 8, command = None)
help_btn.pack()
help_btn.place(y = 45, x = 1290)
English_Flag = PhotoImage(file = 'EnglishFlag.gif')
English_Flag_btn = Button(window, image = English_Flag, command = English_Home_Screen)
English_Flag_btn.pack(side = LEFT, padx = 10)
English_Flag_btn.place(y = 350, x = 300)
Polish_Flag = PhotoImage(file = 'PolishFlag.gif')
Polish_Flag_btn = Button(window, image = Polish_Flag, command = EngFlag)
Polish_Flag_btn.pack(side = LEFT, padx = 10)
Polish_Flag_btn.place(y = 350, x = 600)
Italian_Flag = PhotoImage(file = 'ItalianFlag.gif')
Italian_Flag_btn = Button(window, image = Italian_Flag, command = None)
Italian_Flag_btn.pack(side = LEFT, padx = 10)
Italian_Flag_btn.place(y = 350, x = 900)
window.mainloop()
def English_Home_Screen():
global Eng_window
Eng_window = Tk()
Eng_window.geometry('1366x768')
Eng_window.configure(background = Background_Colour)
Eng_window.title('Local Languages Learning System')
Eng_window.resizable(width=False, height=False)
window.destroy()
Title1 = Label(Eng_window, text = 'Local Languages\nEnglish Home Screen', bg = Background_Colour)
Title1.config(font=("Courier", Title_Text))
Title1.pack()
Title1.place(y = 1, x = 450)
Log_Out = Button(Eng_window, text = 'Log-Out', height = 1, width = 8, command = LogOut1)
Log_Out.pack()
Log_Out.place(y = 5, x = 1290)
Back = Button(Eng_window, text = 'Back', height = 1, width = 8, command = back1)
Back.pack()
Back.place(y = 5, x = 1210)
help_btn = Button(Eng_window, text = 'Help', height = 1, width = 8, command = None)
help_btn.pack()
help_btn.place(y = 45, x = 1290)
Play_all = Button(Eng_window, text = 'Play All Games', height = 2, width = 20, command = None)
Play_all.pack()
Play_all.place(y = 100, x = 320)
Multiple_Choice = Button(Eng_window, text = 'Play Multiple Choice Game', height = 2, width = 20, command = None)
Multiple_Choice.pack()
Multiple_Choice.place(y = 100, x = 510)
Picture = Button(Eng_window, text = 'Play Picture Game', height = 2, width = 20, command = None)
Picture.pack()
Picture.place(y = 100, x = 700)
Memory = Button(Eng_window, text = 'Play Memory Game', height = 2, width = 20, command = None)
Memory.pack()
Memory.place(y = 100, x = 890)
LeaderBoard = Button(Eng_window, text = 'LeaderBoard', height = 2, width = 20, command = None)
LeaderBoard.pack()
LeaderBoard.place(y = 160, x = 600)
Home_Screen()
Apologies for it being so long but to actually see the problem you have to have all the code. Any help or fixies would be great.
Now there are several issues with your code and some I will address.
First off what you are trying to do is very complicated when using a Non-OOP method. I have re-written your code as OOP to provide some ease of use and readability.
For the most part you should avoid using global the easiest way to do this is to use class attributes instead. This allows for you to have a variable that can be seen by any method within the class.
Some helpful tips:
Do not import tkinter twice.
Instead of doing:
import tkinter
from tkinter import *
from tkinter import messagebox as box
Do this instead:
import tkinter as tk
from tkinter import messagebox
Your Try/Except function will not work as you expect as Eng_window.destroy() will not return an error regardless of if the window is there or not. So it will always attempt to destroy Eng_window and never window
Instead do this:
# This will check if the instance of Eng_self_window exist first.
if tk.Toplevel.winfo_exists(self.Eng_self_window):
self.Eng_self_window.destroy()
else:
self.window.destroy()
Let me know if you have any questions on the class set up.
It can be done using global's but its not as clean and harder to deal with.
Here I have moved your code into a class and rearranged some stuff.
import tkinter as tk
from tkinter import messagebox
class Home_Screen(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.Title_Text = 25
self.Title_Font = "Courier"
self.Font = 'Ariel'
self.Rest_Text = 16
self.Background_Colour = 'Light blue'
self.window = parent
self.window.geometry('1366x768')
self.window.configure(background = self.Background_Colour)
self.window.title('Local Languages Learning System')
#self.window.resizable(width=False, height=False)
Title1 = tk.Label(self.window, text = 'Local Languages Home Screen', bg = self.Background_Colour)
Title1.config(font=("Courier", self.Title_Text))
Title1.pack()
Title1.place(y = 1, x = 450)
Question_Label = tk.Label(self.window, text = 'Please Pick The Language You Wish To Learn', bg = self.Background_Colour)
Question_Label.config(font=(self.Font, self.Rest_Text))
Question_Label.pack()
Question_Label.place(y = 200, x = 495)
Log_Out = tk.Button(self.window, text = 'Log-Out', height = 1, width = 8, command = self.LogOut1)
Log_Out.pack()
Log_Out.place(y = 5, x = 1290)
help_btn = tk.Button(self.window, text = 'Help', height = 1, width = 8, command = None)
help_btn.pack()
help_btn.place(y = 45, x = 1290)
self.English_Flag = tk.PhotoImage(file = 'EnglishFlag.gif')
self.English_Flag_btn = tk.Button(self.window, image = self.English_Flag, command = self.English_Home_Screen)
self.English_Flag_btn.pack(side = tk.LEFT, padx = 10)
self.English_Flag_btn.place(y = 350, x = 300)
self.Polish_Flag = tk.PhotoImage(file = 'PolishFlag.gif')
self.Polish_Flag_btn = tk.Button(self.window, image = self.Polish_Flag, command = self.EngFlag)
self.Polish_Flag_btn.pack(side = tk.LEFT, padx = 10)
self.Polish_Flag_btn.place(y = 350, x = 600)
self.Italian_Flag = tk.PhotoImage(file = 'ItalianFlag.gif')
self.Italian_Flag_btn = tk.Button(self.window, image = self.Italian_Flag, command = None)
self.Italian_Flag_btn.pack(side = tk.LEFT, padx = 10)
self.Italian_Flag_btn.place(y = 350, x = 900)
def English_Home_Screen(self):
self.Eng_self_window = tk.Toplevel(self.window)
self.Eng_self_window.geometry('1366x768')
self.Eng_self_window.configure(background = self.Background_Colour)
self.Eng_self_window.title('Local Languages Learning System')
#Eng_self.window.resizable(width=False, height=False)
Title1 = tk.Label(self.Eng_self_window, text = 'Local Languages\nEnglish Home Screen', bg = self.Background_Colour)
Title1.config(font=("Courier", self.Title_Text))
Title1.pack()
Title1.place(y = 1, x = 450)
Log_Out = tk.Button(self.Eng_self_window, text = 'Log-Out', height = 1, width = 8, command = self.LogOut1)
Log_Out.pack()
Log_Out.place(y = 5, x = 1290)
Back = tk.Button(self.Eng_self_window, text = 'Back', height = 1, width = 8, command = self.back1)
Back.pack()
Back.place(y = 5, x = 1210)
help_btn = tk.Button(self.Eng_self_window, text = 'Help', height = 1, width = 8, command = None)
help_btn.pack()
help_btn.place(y = 45, x = 1290)
Play_all = tk.Button(self.Eng_self_window, text = 'Play All Games', height = 2, width = 20, command = None)
Play_all.pack()
Play_all.place(y = 100, x = 320)
Multiple_Choice = tk.Button(self.Eng_self_window, text = 'Play Multiple Choice Game', height = 2, width = 20, command = None)
Multiple_Choice.pack()
Multiple_Choice.place(y = 100, x = 510)
Picture = tk.Button(self.Eng_self_window, text = 'Play Picture Game', height = 2, width = 20, command = None)
Picture.pack()
Picture.place(y = 100, x = 700)
Memory = tk.Button(self.Eng_self_window, text = 'Play Memory Game', height = 2, width = 20, command = None)
Memory.pack()
Memory.place(y = 100, x = 890)
LeaderBoard = tk.Button(self.Eng_self_window, text = 'LeaderBoard', height = 2, width = 20, command = None)
LeaderBoard.pack()
LeaderBoard.place(y = 160, x = 600)
def EngFlag(self):
print('Hello world')
def LogOut1(self):
response = messagebox.askquestion ('?', 'Are You Sure?')
if response == 'yes':
if tk.Toplevel.winfo_exists(self.Eng_self_window):
self.Eng_self_window.destroy()
else:
self.window.destroy()
def back1(self):
print("Go back")
self.Eng_self_window.destroy()
root = tk.Tk()
Home_Screen(root)
root.mainloop()
Quick fix; Don't use multiple instances of Tk. Instead, use Toplevel and hide & show the root window. Add:
...
Background_Colour = 'Light blue'
root = Tk()
root.withdraw()
...
and replace:
...
if response == 'yes':
try:
window.destroy()
except:
Eng_window.destroy()
...
global window
window = Tk()
...
global Eng_window
Eng_window = Tk()
with:
...
if response == 'yes':
root.destroy()
...
global window
window = Toplevel()
...
global Eng_window
Eng_window = Toplevel()

tkinter report frame as well as mouse coordinates

def mouse_click(event):
#'reports' to both terminal and results text box
parent_name = Frame.winfo_parent(root)
parent = Frame._nametowidget(parent_name)
result = ("{0}: {1},{2}\n ".format('Clicked at', event.x, event.y))
print('clicked at',parent, event.x, event.y)
from tkinter import *
root = Tk()
root.title("Change Text")
root.geometry('700x500')
top=root.winfo_toplevel()
for rn in range(0,9): # rn = row number
top.rowconfigure(rn, weight=1)
top.rowconfigure(rn, weight=0)
for cn in range(0,5): # cn = column number
top.columnconfigure(cn, weight=1)
# FRAME 1
frame1 = Frame(root, borderwidth = 2, background = '#EFE0CD', relief = RIDGE,width = 25, height = 20)
frame1.bind("<Button-1>", mouse_click)
frame1.grid(column = 0, row = 0, columnspan = 2, rowspan = 3, sticky = N+S+E+W)
frame1_lbl = Label(frame1, text='Frame 1', font='comic-sans-MS 10 ', fg ='red', bg = '#EFE0CD')
frame1_lbl.grid(row=0, column =0)
# FRAME 2
frame2 = Frame(root, borderwidth = 2, background = '#CCC6B0', relief = RIDGE,width = 25, height = 20)
frame2.bind("<Button-1>", mouse_click)
frame2.grid(column = 0, row = 3, columnspan = 2, rowspan = 3, sticky = N+S+E+W)
frame2_lbl = Label(frame2, text='Frame 2', font='comic-sans-MS 10', fg ='red', bg = '#CCC6B0')
frame2_lbl.grid(row=0, column =0,)
root.mainloop()
I know that this is a really dumb question. How do I identify the Frame in which the mouse is clicked so that report prints clicked frame1 or frame2 at x y coordinates?. I found a bit about using winfo_parent but have obviously not used it properly.
Thank you
The event object that is passed in to the callback has a widget attribute which is a reference to the widget that got the event. So, event.widget is what you want to print.

Resources