'Python, tkinter, scrollbar not working after new instance is created
So, my coding is coming along but I wouldn't dare to take a fully object oriented approach to this program.
I am trying to build a mock CRM system for a talent agency and have run into a serious issue. I can't figure out for the life of me what is wrong, but it's to do with the scrollbars on my tkinter canvasses.
Essentially what is happening is that my scrollbar works fine to scroll the 'venues' tab, but when I click onto another tab (destroying and recreating the instances that were there), the scrollbar goes white and stops working.
I think I have the same scrollbar configuration for each tab, I basically copy + pasted the code so that it would work the same.
Can anyone shed light on what is happening? I am attaching screenshots and the code.
from tkinter import *
from tkinter import ttk
import mysql.connector
db = mysql.connector.connect(
host='localhost',
user='root',
passwd='**********',
database='databaseagency',
)
mycursor = db.cursor()
root = Tk()
root.title('Superposer v0.1')
root.geometry('2560x1440+0+0')
results_canvas = Canvas(root)
scrollbar = ttk.Scrollbar(root)
results_frame = Frame(results_canvas)
new_button = Button(root)
map_button = Button(root)
active_tab = ''
#MAPS API = MAPBOX
def new_record():
global active_tab
def clear_fields():
alias_entry.delete(0, END)
profession_entry.delete(0, END)
number_entry.delete(0, END)
email_entry.delete(0, END)
forename_entry.delete(0, END)
surname_entry.delete(0, END)
first_line_address_entry.delete(0, END)
city_entry.delete(0, END)
postcode_entry.delete(0, END)
dob_entry.delete(0, END)
ni_entry.delete(0, END)
def add_record():
sql_command = 'INSERT INTO Talents \
(Alias, Profession, `Contact Number`, `Email Address`, Forename, Surname, `Address (First Line)`, \
`Address (City)`, `Post Code`, `Date of Birth`, `National Insurance`) \
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'
values = (alias_entry.get(),
profession_entry.get(),
number_entry.get(),
email_entry.get(),
forename_entry.get(),
surname_entry.get(),
first_line_address_entry.get(),
city_entry.get(),
postcode_entry.get(),
dob_entry.get(),
ni_entry.get())
mycursor.execute(sql_command, values)
db.commit()
clear_fields()
new_record_window = Tk()
new_record_window.title('New Record')
new_record_window.geometry('420x512+0+0')
if active_tab == 'Talent':
alias_label = Label(new_record_window, text='Alias').grid(row=0, column=0, sticky=W, padx=10)
profession_label = Label(new_record_window, text='Profession').grid(row=1, column=0, sticky=W, padx=10)
number_label = Label(new_record_window, text='Phone Number').grid(row=2, column=0, sticky=W, padx=10)
email_label = Label(new_record_window, text='Email Address').grid(row=3, column=0, sticky=W, padx=10)
forename_label = Label(new_record_window, text='Forename').grid(row=4, column=0, sticky=W, padx=10)
surname_label = Label(new_record_window, text='Surname').grid(row=5, column=0, sticky=W, padx=10)
first_line_address_label = Label(new_record_window, text='Address (First Line)').grid(row=6, column=0,
sticky=W, padx=10)
city_label = Label(new_record_window, text='City').grid(row=7, column=0, sticky=W, padx=10)
postcode_label = Label(new_record_window, text='Postcode').grid(row=8, column=0, sticky=W, padx=10)
dob_label = Label(new_record_window, text='Date of Birth').grid(row=9, column=0, sticky=W, padx=10)
ni_label = Label(new_record_window, text='National Insurance').grid(row=10, column=0, sticky=W, padx=10)
alias_entry = Entry(new_record_window)
alias_entry.grid(row=0, column=1, sticky=W, pady=10)
profession_entry = Entry(new_record_window)
profession_entry.grid(row=1, column=1, sticky=W, pady=10)
number_entry = Entry(new_record_window)
number_entry.grid(row=2, column=1, sticky=W, pady=10)
email_entry = Entry(new_record_window)
email_entry.grid(row=3, column=1, sticky=W, pady=10)
forename_entry = Entry(new_record_window)
forename_entry.grid(row=4, column=1, sticky=W, pady=10)
surname_entry = Entry(new_record_window)
surname_entry.grid(row=5, column=1, sticky=W, pady=10)
first_line_address_entry = Entry(new_record_window)
first_line_address_entry.grid(row=6, column=1, sticky=W, pady=10)
city_entry = Entry(new_record_window)
city_entry.grid(row=7, column=1, sticky=W, pady=10)
postcode_entry = Entry(new_record_window)
postcode_entry.grid(row=8, column=1, sticky=W, pady=10)
dob_entry = Entry(new_record_window)
dob_entry.grid(row=9, column=1, sticky=W, pady=10)
ni_entry = Entry(new_record_window)
ni_entry.grid(row=10, column=1, sticky=W, pady=10)
add_record_button = Button(new_record_window, text='Add Record', command=add_record)
add_record_button.grid(row=11, column=1, padx=10, pady=10)
clear_fields_button = Button(new_record_window, text='Clear Fields', command=clear_fields)
clear_fields_button.grid(row=11, column=0, padx=10, pady=10)
def list_talent():
global new_button, map_button, scrollbar, results_canvas, active_tab, results_frame
results_canvas.destroy()
scrollbar.destroy()
new_button.destroy()
map_button.destroy()
results_frame.destroy()
active_tab = ''
results_canvas = Canvas(root)
results_canvas.grid(row=4, column=0, columnspan=7)
scrollbar = ttk.Scrollbar(root, orient=VERTICAL, command=results_canvas.yview)
scrollbar.grid(row=4, column=8, sticky=N+S)
results_frame = Frame(results_canvas)
results_canvas.create_window((0, 0), window=results_frame, anchor="nw")
new_button = Button(root, text='New', font=('Times New Roman', 12), command=new_record)
new_button.grid(row=5, column=0)
map_button = Button(root, text='Map', font=('Times New Roman', 12))
map_button.grid(row=5, column=7)
results_title = ['ID', 'Name', 'Profession', 'City', 'Date Signed', 'Phone Number', 'Quick Note']
for index, text in enumerate(results_title):
res_title_label = Label(results_frame, text=text)
res_title_label.grid(row=0, column=index)
mycursor.execute("SELECT * FROM talents")
result = mycursor.fetchall()
for index, x in enumerate(result):
results_label = Label(results_frame, text=x[0])
results_label.grid(row=1+index, column=0, padx=10)
results_label = Label(results_frame, text=x[3])
results_label.grid(row=1+index, column=1, padx=10)
results_label = Label(results_frame, text=x[4])
results_label.grid(row=1+index, column=2, padx=10)
results_label = Label(results_frame, text=x[10])
results_label.grid(row=1+index, column=3, padx=10)
results_label = Label(results_frame, text=x[5])
results_label.grid(row=1+index, column=4, padx=10)
results_label = Label(results_frame, text=x[7])
results_label.grid(row=1+index, column=5, padx=10)
results_label = Label(results_frame, text=x[1])
results_label.grid(row=1+index, column=6, padx=10)
edit_button = Button(results_frame, text='E', font=('Times New Roman', 12))
edit_button.grid(row=1+index, column=7)
view_button = Button(results_frame, text='V', font=('Times New Roman', 12))
view_button.grid(row=1+index, column=8)
results_canvas.configure(yscrollcommand=scrollbar.set)
results_canvas.bind('<Configure>', lambda e: results_canvas.configure(scrollregion=results_canvas.bbox("all")))
active_tab = 'Talent'
def list_venues():
global new_button, map_button, scrollbar, results_canvas, active_tab, results_frame
results_canvas.destroy()
scrollbar.destroy()
new_button.destroy()
map_button.destroy()
results_frame.destroy()
active_tab = ''
results_canvas = Canvas(root)
results_canvas.grid(row=4, column=0, columnspan=7)
scrollbar = ttk.Scrollbar(root, orient=VERTICAL, command=results_canvas.yview)
scrollbar.grid(row=4, column=8, sticky=N+S)
results_frame = Frame(results_canvas)
results_canvas.create_window((0, 0), window=results_frame, anchor="nw")
new_button = Button(root, text='New', font=('Times New Roman', 12), command=new_record)
new_button.grid(row=5, column=0)
map_button = Button(root, text='Map', font=('Times New Roman', 12))
map_button.grid(row=5, column=7)
results_title = ['ID', 'Name', 'City', 'Genres', 'Phone Number', 'Email Address', 'Quick Note']
for index, text in enumerate(results_title):
res_title_label = Label(results_frame, text=text)
res_title_label.grid(row=0, column=index)
mycursor.execute("SELECT * FROM venues")
result = mycursor.fetchall()
for index, x in enumerate(result):
results_label = Label(results_frame, text=x[0])
results_label.grid(row=1+index, column=0, padx=10)
results_label = Label(results_frame, text=x[1])
results_label.grid(row=1+index, column=1, padx=10)
results_label = Label(results_frame, text=x[3])
results_label.grid(row=1+index, column=2, padx=10)
results_label = Label(results_frame, text=x[12])
results_label.grid(row=1+index, column=3, padx=10)
results_label = Label(results_frame, text=x[5])
results_label.grid(row=1+index, column=4, padx=10)
results_label = Label(results_frame, text=x[6])
results_label.grid(row=1+index, column=5, padx=10)
results_label = Label(results_frame, text=x[4])
results_label.grid(row=1+index, column=6, padx=10)
edit_button = Button(results_frame, text='E', font=('Times New Roman', 12))
edit_button.grid(row=1+index, column=7)
view_button = Button(results_frame, text='V', font=('Times New Roman', 12))
view_button.grid(row=1+index, column=8)
results_canvas.configure(yscrollcommand=scrollbar.set)
results_canvas.bind('<Configure>', lambda e: results_canvas.configure(scrollregion=results_canvas.bbox("all")))
active_tab = 'Venues'
def list_bookings():
global new_button, map_button, scrollbar, results_canvas, active_tab, results_frame
results_canvas.destroy()
scrollbar.destroy()
new_button.destroy()
map_button.destroy()
results_frame.destroy()
active_tab = ''
results_canvas = Canvas(root)
results_canvas.grid(row=4, column=0, columnspan=7)
scrollbar = ttk.Scrollbar(root, orient=VERTICAL, command=results_canvas.yview)
scrollbar.grid(row=4, column=8, sticky=N+S)
results_frame = Frame(results_canvas)
results_canvas.create_window((0, 0), window=results_frame, anchor="nw")
new_button = Button(root, text='New', font=('Times New Roman', 12), command=new_record)
new_button.grid(row=5, column=0)
map_button = Button(root, text='Map', font=('Times New Roman', 12))
map_button.grid(row=5, column=7)
results_title = ['ID', 'Venue', 'Artist 1', 'Artist 2', 'Datetime', 'Status', 'Quick Note']
for index, text in enumerate(results_title):
res_title_label = Label(results_frame, text=text)
res_title_label.grid(row=0, column=index)
mycursor.execute("SELECT * FROM liveperformances")
result = mycursor.fetchall()
for index, x in enumerate(result):
results_label = Label(results_frame, text=x[0])
results_label.grid(row=1+index, column=0, padx=10)
results_label = Label(results_frame, text=x[3])
results_label.grid(row=1+index, column=1, padx=10)
results_label = Label(results_frame, text=x[4])
results_label.grid(row=1+index, column=2, padx=10)
results_label = Label(results_frame, text=x[5])
results_label.grid(row=1+index, column=3, padx=10)
results_label = Label(results_frame, text=x[11])
results_label.grid(row=1+index, column=4, padx=10)
results_label = Label(results_frame, text=x[2])
results_label.grid(row=1+index, column=5, padx=10)
results_label = Label(results_frame, text=x[1])
results_label.grid(row=1+index, column=6, padx=10)
edit_button = Button(results_frame, text='E', font=('Times New Roman', 12))
edit_button.grid(row=1+index, column=7)
view_button = Button(results_frame, text='V', font=('Times New Roman', 12))
view_button.grid(row=1+index, column=8)
results_canvas.configure(yscrollcommand=scrollbar.set)
results_canvas.bind('<Configure>', lambda e: results_canvas.configure(scrollregion=results_canvas.bbox("all")))
active_tab = 'Bookings'
#TITLE
title_label = Label(root, text='Superposer v1.0.0', font=('Times New Roman', 28))
title_label.grid(row=0, column=0, columnspan=3, pady='10')
#TAB BUTTONS
talent_button = Button(root, text='Talent', font=('Times New Roman', 16), command=list_talent)
talent_button.grid(row=1, column=0, columnspan=2, pady='10')
venues_button = Button(root, text='Venues', font=('Times New Roman', 16), command=list_venues)
venues_button.grid(row=1, column=3, columnspan=2, pady='10')
bookings_button = Button(root, text='Bookings', font=('Times New Roman', 16), command=list_bookings)
bookings_button.grid(row=1, column=6, columnspan=2, pady='10')
#SEARCH BAR
search_bar = Entry(root)
search_bar.grid(row=2, column=0, columnspan=7, pady='10', sticky=W+E)
search_button = Button(root, text='Search', font=('Times New Roman', 16))
search_button.grid(row=2, column=8, pady='10')
#FILTERS_SORTS
filter_button = Button(root, text='Filter', font=('Times New Roman', 12))
filter_button.grid(row=3, column=6)
sort_button = Button(root, text='Sort', font=('Times New Roman', 12))
sort_button.grid(row=3, column=7)
root.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


