Scrollbar not showing - tkinter - python-3.x

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.

Related

TKinter Scrollbar - Problem with too many scrollbars

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()

Trouble with place_forget()

I have been trying to get .place_forget() to work in my test application but I am having some difficulty.
I am trying to get the widgets in "def select_1:" to show when R1 "Yes" checkbox is checked and then have the widgets not show when the check is removed.
The widgets show when I check the box but will not forget(hide) when it is then unchecked.
Any assistance would be greatly appreciated.
import os, sys
import pywintypes
from tkinter import *
#================================= MAIN WINDOW =================================
root = Tk()
w = 750
h = 325
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root.title('Test Application')
root.geometry('750x325')
root.config(bg='#000000')
pass_entry=StringVar()
pass_1=StringVar()
var1 = IntVar()
var2 = IntVar()
selection = StringVar()
#=================================== HEADER ====================================
header = Label(root, font = ('Times New Roman','25','bold'), text='Test Application',
bg = ('#000000'), fg = ('#B3CDE0'))
header.place(x=250,y=15)
#================================ RESULT WINDOW ================================
result_window = Text(root, width = 80, height = 10)
result_window.place(x=50, y=75)
#=========================== CHECK BUTTON SELECTION ============================
def select_1():
if var1.get()==1:
pass_entry = Entry(root, width = 20, textvariable = pass_1, bg='#000000', fg='#B3CDE0')
pass_entry.place(x=250,y=275)
pass_entry.focus_set()
selection = Label(root, text='Enter OS Password:', bg='#000000', fg='#B3CDE0')
selection.place(x=140,y=275)
elif var1.get()==0:
pass_entry.place_forget()
selection.place_forget()
else:
return
#========================= ACCESS CREDENTIAL MANAGER ===========================
def getpass():
if var1.get()==1:
os.system('explorer.exe')
else:
return
def close():
root.destroy()
#=============================== RADIO BUTTONS =================================
R1 = Checkbutton(root, text="Yes", variable = var1, onvalue = 1, offvalue = 0, height=1, width = 15, activebackground = '#000000', bg='#000000', fg='#B3CDE0', command=select_1)
R1.place(x=350,y=250)
R2 = Checkbutton(root, text="No ", variable = var2, onvalue = 1, offvalue = 0, height=1, width = 15, bg='#000000', fg='#B3CDE0', activebackground = '#000000')
R2.place(x=350,y=275)
#=========================== RADIO BUTTON SELECTION ============================
cancel_button = Button(root, text='Cancel', width = 12, bg=('#000000'), fg = ('#B3CDE0'), activebackground = '#000000', command = close)
cancel_button.place(x=590,y=270)
recover_button = Button(root, text='Open', width = 12, bg=('#000000'), fg = ('#B3CDE0'), activebackground = '#000000',command = getpass)
recover_button.place(x=480,y=270)
root.mainloop()
When you want to make a widget disappear and then appear again, you don't actually have to define it and all it's options again.
#=========================== CHECK BUTTON SELECTION ============================
pass_entry = Entry(root, width = 20, textvariable = pass_1, bg='#000000', fg='#B3CDE0')
selection = Label(root, text='Enter OS Password:', bg='#000000', fg='#B3CDE0')
def select_1():
if var1.get()==1:
pass_entry.place(x=250,y=275)
pass_entry.focus_set()
selection.place(x=140,y=275)
elif var1.get()==0:
pass_entry.place_forget()
selection.place_forget()
else:
return
Notice how pass_entry and selection are defined outside of the function and only placed inside the function.
What's a bit confusing though is why you're using two separate buttons for "yes" and "no" if checking them doesn't automatically uncheck the other one (i assume that's the purpose of the two buttons).
If that's what you want to do, you should use the Radiobutton widget instead. Notice how both of them affect the same variable and each of them have one value.
#=============================== RADIO BUTTONS =================================
R1 = Radiobutton(root, text="Yes", variable = var1, value = 1, height=1, width = 15, activebackground = '#000000', bg='#000000', fg='#B3CDE0', command=select_1)
R1.place(x=350,y=250)
R2 = Radiobutton(root, text="No", variable = var1, value = 0, height=1, width = 15, activebackground = '#000000', bg='#000000', fg='#B3CDE0', command=select_1)
R2.place(x=350,y=275)
I also tidied up the other options a bit too, you had some random spacing in there.

open a window that was destroyed previously in Tkinker

Iam new to Tkinter and am trying do an interface a small randomising application. i have several windows and i need to open one at a time, however the problem is when a user enters wrong crentials and they have to go back to the login page which was destroyed previous. When i run my code so far, i get the error below:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\SANDRA\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 1705, in call
return self.func(*args)
File "C:\Users\SANDRA\Desktop\python work\GUI\stem page 1.py", line 31, in validation
open_login()
File "C:\Users\SANDRA\Desktop\python work\GUI\stem page 1.py", line 35, in open_login
root.destroy()
File "C:\Users\SANDRA\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 2062, in destroy
self.tk.call('destroy', self._w)
_tkinter.TclError: can't invoke "destroy" command: application has been destroyed
below is my code so far:
from tkinter import *
#creating the admin's logged in page
def admin_logged_in():
#global adn_logged_in
adn_logged_in = Tk()
adn_logged_in.title("Administrative page")
adn_logged_in.config(bg = "#ffe6ff")
adn_logged_in.geometry("700x400")
adn_logged_in.resizable(width=FALSE, height=FALSE)
adn_lbl = Label(adn_logged_in, text ="please select what you wish to do.", font = "Arial 16",fg = "black", bg = "#ffe6ff", pady = 50)
adn_lbl.pack()
list = Listbox(adn_logged_in,selectmode = SINGLE, height = 2,bg = "#ffe6ff", width = 30, font = "Arial 16" ,fg = "blue")
list.insert(1, "Add students")
list.insert(2, "Delete students")
list.pack()
adn_logged_in.mainloop()
#creating a function to validate the username and password
def validation():
username_text = username.get()
password_text = password.get()
if username_text == "admin" and password_text == "123":
admin.destroy()
admin_logged_in()
else:
open_login()
#creating a function to open the administrator window
def open_login():
root.destroy()
global password
global username
global admin
admin = Tk()#creates a new window
admin.title("Administrative login page")
admin.config(bg = "#ffe6ff")
admin.geometry("700x400")
admin.resizable(width=FALSE, height=FALSE)
label1 = Label(admin,text = " please login", font = "Arial 40",fg = "blue", bg = "#ffe6ff", pady = 40)
label1.pack()
open_frame = Frame(admin, bg = "#ffe6ff")
open_frame.pack()
#crating label and entry for user name .
name_lbl = Label(open_frame, text = "Username: ",font = "Arial 16", fg = "black",bg = "#ffe6ff",)
name_lbl.grid(row = 0, column = 0, sticky = "e")
username = StringVar()
name_entry = Entry(open_frame, textvariable = username, width = 50)
name_entry.grid(row = 0, column = 1, sticky = "w", pady =8)
#creating label and entry for password
pw_lbl = Label(open_frame, text = "Password: ",font = "Arial 16", fg = "black", bg = "#ffe6ff",)
pw_lbl.grid(row = 1, column = 0, sticky = "e")
password = StringVar()
pw_entry = Entry(open_frame, textvariable = password,width = 50, show = "*")
pw_entry.grid(row = 1, column = 1, sticky = "w",pady = 8)
#creating a submit button
sub_btn = Button(open_frame, text = "Submit",font = "Arial 16", fg = "black", bg = "#ffe6ff", width = 20,command= validation)
sub_btn.grid(row = 3, column = 1, sticky = "s")
admin.mainloop()
#creating the main window
root = Tk()
root.title("Student Randomiser v 1.0")
root.config(bg = "#ffe6ff")
root.geometry("700x400")
root.resizable(width=FALSE, height=FALSE)
#creating the frame1 for the first window
frame1 = Frame(relief = FLAT, borderwidth = 3, bg ="#ffe6ff")
frame1.pack()
#creating the heading
main_lbl = Label(frame1, text = "STUDENT RANDOMISER",bg = "#ffe6ff",fg = "blue", font = "Helvetica 40 bold")
main_lbl.pack(side = TOP, pady= 40)
#creating the welcome note
welcome_lbl = Label(frame1,font = "Arial 18", text = "Welcome to our student randomising sytem ",bg = "#ffe6ff",fg = "black")
welcome_lbl.pack()
#creatinf empty frame for spacing
empty_frame = Frame(height = 40)
empty_frame.pack()
#creating a frame for the buttons
frame2 = Frame(relief = FLAT,borderwidth = 3, bg ="#ffe6ff")
frame2.pack()
#creating a adminstrator button
adm_button = Button(frame2, font = "Arial 16",text = "Adminstrator", fg = "black", width = 20, command =open_login)
adm_button.pack(side = LEFT, padx = 15, pady = 5)
#creating user button
user_button = Button(frame2, font = "Arial 16", text = "User", fg = "black", width = 20)
user_button.pack(side = RIGHT, padx = 15, pady = 5)
root.mainloop()

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