Related
Application: Building a notes application (as an intro to GUI development in Python) which includes feature of a scrollbar to scroll through a textbox
Problem: I can't actually seem to scroll down through the textbox. I don't seem to get the grayed rectangle which lets me control the scrollbar and scroll up/down through the textbox
#importing necessary packages
from tkinter import *
from tkinter import font
from tkinter import ttk
#set up main window
root = Tk()
root.title("Notes")
root.geometry("400x650")
#functions
#functions to change all widget button's backgrounds when user hovers over it and leaves it
def enter_button(e):
e.widget.config(background = "#D4D4D4")
#SystemButtonFace is default colour
def leave_button(e):
e.widget.config(background = "SystemButtonFace")
#clear text in text-box
def clear():
#delete all text from text_box
text_box.delete(1.0,END)
def bold_it():
#create font
try:
bold_font = font.Font(text_box, text_box.cget("font"))
bold_font.configure(weight = "bold")
#creating tag called "bold" which bolds textll upon condition
text_box.tag_configure("bold", font = bold_font)
#creating a bold tag which highlights first character
bold_tag = text_box.tag_names("sel.first")
#condition for checking to see if tag is applied or not
#in the first highlighted character
#if tag is applied, remove the bold from first-highlighted text
#- last highlighted text
#"bold" needs to be matched in the tag
if "bold" in bold_tag:
text_box.tag_remove("bold","sel.first","sel.last")
else:
text_box.tag_add("bold","sel.first", "sel.last")
except TclError:
pass
def italics_it():
try:
#create a font
italics_font = font.Font(text_box, text_box.cget("font"))
italics_font.configure(slant = "italic")
#create a tag called "italic"
text_box.tag_configure("italics", font = italics_font)
italics_tag = text_box.tag_names("sel.first")
#condition to see whether tag has been applies or not
if "italics" in italics_tag:
text_box.tag_remove("italics", "sel.first","sel.last")
else:
text_box.tag_add("italics", "sel.first", "sel.last")
except TclError:
pass
#frames
top_frame = LabelFrame(root, padx = 30, pady = 10)
button_frame = LabelFrame(root, padx = 30, pady = 10)
text_frame = LabelFrame(root, padx = 10, pady = 10)
bottom_frame = LabelFrame(root, borderwidth = 0, highlightthickness = 5)
top_frame.grid(row = 0 , column = 0)
button_frame.grid(row = 1, column = 0, pady = 10)
text_frame.grid(row = 2, column = 0, pady = 1)
bottom_frame.grid(row = 3, column = 0, pady = 3)
#labels, textboxes, buttons
#top_frame content
Notes_label = Label(top_frame, text = "Notes", fg = "black", font = 1, padx = 141)
Notes_label.grid(row = 0 , column = 0)
save_button = Button(top_frame, text = "save")
#padx increases distance between buttons
#button_frame content
#bold button
#the ideal is that if u press ctrl + b, the bold_button is pressed by itself
#rn, it's gonna be a highlight technique
bold_button = Button(button_frame, text = "B", padx = 4, pady = 2, command = bold_it)
bold_button.grid(row = 0, column = 0)
#italicsize button
italics_button = Button(button_frame, text = "I", padx = 4, pady = 2, command = italics_it)
italics_button.grid(row = 0, column = 2, padx = 15)
#text_box frame button
text_box = Text(text_frame, width = 45, height = 27)
text_box.grid(row = 0, column = 0)
#text_box frame content
main_scrollbar = ttk.Scrollbar(text_frame, orient = "vertical", command = text_box.yview)
main_scrollbar.grid(row = 0, column = 1)
text_box["yscrollcommand"] = main_scrollbar.set
clear_button = Button(bottom_frame, text = "clear", padx = 2, pady = 2, command = clear)
clear_button.grid(row = 0, column = 0, padx = 15, pady = 10)
save_button = Button(bottom_frame, text = "save note", padx = 2, pady = 2)
save_button.grid(row = 0, column =1, padx = 15, pady = 10)
#binding all buttons for changing colours when user hovers over it and leaves it
bold_button.bind("<Enter>", enter_button)
bold_button.bind("<Leave>", leave_button)
italics_button.bind("<Enter>", enter_button)
italics_button.bind("<Leave>", leave_button)
clear_button.bind("<Enter>", enter_button)
clear_button.bind("<Leave>", leave_button)
save_button.bind("<Enter>", enter_button)
save_button.bind("<Leave>", leave_button)
# main program loop
root.mainloop()
here's an image of the problem image of problem
I would also be very grateful if one could explain the concept of scrollbar.set and like yview and why they are both needed for the scrollbar to work. Tutorials and videos don't seem to explain the concept, but just implement it
In line 145. You're missing sticky
main_scrollbar.grid(row = 0, column = 1, sticky=NS)
Output:
I noticed a lagging behavior when I dragged my Tkinter Root window across the screen (it does not have same motion speed as my mouse cursor). I have created a simple Tkinter app where one of the features is that user can select via button widget(s) to show/hide different Frame widgets which contains other child widgets. I have created all the required widget(s) during init() in a class.
In this example, lets say I have 4 Frame widgets (Frame A, B, C, D) and 4 buttons (Button A, B, C, D), respectively. Each Frame widget(s) contain several child widget(s). So, when I click Button A, the app will show Frame A using .grid(), and hide other Frame(s) using .grid_remove(). However, when I continue my clicking actions on the rest of the Button(s) to display & hide other Frame(s), I noticed it affected my Root window dragging motion when I want to move the app across my screen.
Code:
import tkinter as tk
from functools import partial
from tkinter import ttk
class AppGui(tk.Frame):
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master, *args, **kwargs)
self.grid_columnconfigure(index = 0, weight = 1)
self.grid_rowconfigure(index = 1, weight = 1)
self.main_interface()
def main_interface(self):
prnt = tk.Frame(self, highlightthickness = 0, bd = 0)
self.btn_a = tk.Button(prnt, relief = tk.GROOVE, text = 'Frame A', width = 12, font='Helvetica 11 bold')
self.btn_b = tk.Button(prnt, relief = tk.GROOVE, text = 'Frame B', width = 12, font='Helvetica 11 bold')
self.btn_c = tk.Button(prnt, relief = tk.GROOVE, text = 'Frame C', width = 12, font='Helvetica 11 bold')
self.btn_d = tk.Button(prnt, relief = tk.GROOVE, text = 'Frame D', width = 12, font='Helvetica 11 bold')
self.btn_a['command'] = partial(self.fr_sel_func, 'A')
self.btn_b['command'] = partial(self.fr_sel_func, 'B')
self.btn_c['command'] = partial(self.fr_sel_func, 'C')
self.btn_d['command'] = partial(self.fr_sel_func, 'D')
self.btn_a.grid(column = 0, row = 0, columnspan = 1, rowspan = 1, padx = (1,5), pady = (1,1), sticky = 'nwse')
self.btn_b.grid(column = 1, row = 0, columnspan = 1, rowspan = 1, padx = (1,5), pady = (1,1), sticky = 'nwse')
self.btn_c.grid(column = 2, row = 0, columnspan = 1, rowspan = 1, padx = (1,5), pady = (1,1), sticky = 'nwse')
self.btn_d.grid(column = 3, row = 0, columnspan = 1, rowspan = 1, padx = (1,1), pady = (1,1), sticky = 'nwse')
self.hashmap_fr_gui = {}
# self.create_fr_gui(): Function to create a Frame widget with all the child widgets.
# Store the Frame widget(s) in a hashmap
self.hashmap_fr_gui['A'] = self.create_fr_gui('A')
self.hashmap_fr_gui['B'] = self.create_fr_gui('B')
self.hashmap_fr_gui['C'] = self.create_fr_gui('C')
self.hashmap_fr_gui['D'] = self.create_fr_gui('D')
prnt.grid(column = 0, row = 0, columnspan = 1, rowspan = 1, padx = (2,1), pady = (1,1), sticky = 'nwse')
self.btn_a.invoke()
def fr_sel_func(self, hashmap_id):
if hashmap_id in self.hashmap_fr_gui:
for kw, tk_gui in self.hashmap_fr_gui.items():
if kw == hashmap_id:
tk_gui.grid(column = 0, row = 1, columnspan = 1, rowspan = 1, padx = (2,2), pady = (3,1), sticky = 'nwse')
else:
tk_gui.grid_remove()
def create_fr_gui(self, hashmap_id):
# Create Parent Frame GUI
prnt_gui = tk.Frame(self)
prnt_gui['bg'] = 'white'
prnt_gui.grid_columnconfigure(index = 0, weight = 1)
prnt_gui.grid_columnconfigure(index = 1, weight = 1)
prnt_gui.grid_rowconfigure(index = 0, weight = 1)
prnt_gui.grid_rowconfigure(index = 1, weight = 1)
child_gui_a = self.create_fr_child(prnt_gui, 'Subframe 1{}'.format(hashmap_id))
child_gui_b = self.create_fr_child(prnt_gui, 'Subframe 2{}'.format(hashmap_id))
child_gui_c = self.create_fr_child(prnt_gui, 'Subframe 3{}'.format(hashmap_id))
child_gui_d = self.create_fr_child(prnt_gui, 'Subframe 4{}'.format(hashmap_id))
child_gui_a.grid(column = 0, row = 0, columnspan = 1, rowspan = 1, padx = (1,1), pady = (1,1), sticky = 'nwse')
child_gui_b.grid(column = 1, row = 0, columnspan = 1, rowspan = 1, padx = (3,1), pady = (1,1), sticky = 'nwse')
child_gui_c.grid(column = 0, row = 1, columnspan = 1, rowspan = 1, padx = (1,1), pady = (3,1), sticky = 'nwse')
child_gui_d.grid(column = 1, row = 1, columnspan = 1, rowspan = 1, padx = (3,1), pady = (3,1), sticky = 'nwse')
return prnt_gui
def create_fr_child(self, prnt, panel_name):
# Create Child Frame GUI
child_gui = tk.Frame(prnt, highlightthickness = 1, highlightbackground = 'black')
child_gui.grid_columnconfigure(index = 0, weight = 1)
child_gui.grid_columnconfigure(index = 1, weight = 10)
child_gui.grid_rowconfigure(index = 0, weight = 0)
child_gui.grid_rowconfigure(index = 1, weight = 1)
name_tk_lb = tk.Label(child_gui, text = panel_name, font = 'Helvetica 14 bold'
, justify = tk.LEFT, anchor = 'nw')
name_tk_lb.grid(column = 0, row = 0, columnspan = 2, rowspan = 1, padx = (5,1), pady = (5,5), sticky = 'nwse')
#####################################################################################################################
# Left Side Grid Widget(s)
left_grid = tk.Frame(child_gui)
left_grid.grid_columnconfigure(index = 0, weight = 1)
left_grid.grid(column = 0, row = 1, columnspan = 1, rowspan = 1, padx = (20,1), pady = (1,1), sticky = 'nwse')
tk_lb = tk.Label(left_grid, text = 'Combobox: ', font = 'Helvetica 12 italic', width = 12
, justify = 'left', anchor = 'w')
tk_lb.grid(column = 0, row = 0, columnspan = 1, rowspan = 1, padx = (1,5), pady = (1,1), sticky = 'nwse')
ttk_cbox = ttk.Combobox(left_grid, values = [], width=13, state='readonly', font = 'Helvetica 11')
ttk_cbox.grid(column = 0, row = 1, columnspan = 1, rowspan = 1, padx = (1,5), pady = (5,1), sticky = 'nwse')
ttk_cbox.unbind_class("TCombobox", "<MouseWheel>")
tk_btn = tk.Button(left_grid, relief = tk.GROOVE, width = 10, height = 1, font = 'Helvetica 12')
tk_btn['text'] = 'Button'
tk_btn.grid(column = 0, row = 2, columnspan = 1, rowspan = 1, padx = (1,5), pady = (100,1), sticky = 'nwse')
#####################################################################################################################
# Right Side Grid Widget(s)
right_grid = tk.Frame(child_gui)
right_grid.grid_columnconfigure(index = 0, weight = 5)
right_grid.grid_columnconfigure(index = 1, weight = 10)
right_grid.grid_columnconfigure(index = 2, weight = 1)
right_grid.grid(column = 1, row = 1, columnspan = 1, rowspan = 1, padx = (10,20), pady = (1,1), sticky = 'nwse')
for i in range(0, 6):
right_grid.grid_rowconfigure(index = i, weight = 1, min = 50)
tk_lb = tk.Label(right_grid, text = 'Scalebar {}'.format(i + 1), font = 'Helvetica 12', justify = 'right', anchor = 'ne')
scl_var = tk.StringVar()
sbox_var = tk.StringVar()
tk_scl = tk.Scale(right_grid, from_=1, to=10, variable=scl_var, orient='horizontal', showvalue=0)
tk_sbox = tk.Spinbox(right_grid, width = 4, textvariable = sbox_var, from_=1, to=10
, highlightbackground="black", highlightthickness=1, font = 'Helvetica 12')
scl_var.set(1)
tk_lb.grid(column = 0, row = i, columnspan = 1, rowspan = 1, padx = (3,1), pady = (3,1), sticky = 'nwes')
tk_scl.grid(column = 1, row = i, columnspan = 1, rowspan = 1, padx = (3,1), pady = (4,1), sticky = 'nwe')
tk_sbox.grid(column = 2, row = i, columnspan = 1, rowspan = 1, padx = (3,3), pady = (3,1), sticky = 'nwe')
return child_gui
if __name__ == '__main__':
tk_root = tk.Tk()
tk_root_width = 890
tk_root_height = 600
screen_width = tk_root.winfo_screenwidth()
screen_height = tk_root.winfo_screenheight()
x_coordinate = int((screen_width/2) - (tk_root_width/2))
y_coordinate = int((screen_height/2) - (tk_root_height/2))
tk_root.geometry("{}x{}+{}+{}".format(tk_root_width, tk_root_height, x_coordinate, y_coordinate))
app_gui = AppGui(tk_root)
tk_root.grid_columnconfigure(index = 0, weight = 1)
tk_root.grid_rowconfigure(index = 0, weight = 1)
app_gui.grid(column = 0, row = 0, columnspan = 1, rowspan = 1, padx = (1,1), pady = (1,1), sticky = 'nwse')
tk_root.mainloop()
The issue seem to be mitigated when i experimented with .grid_destroy() on the Frame widget(s). However, I prefer not to use .grid_destroy(), otherwise I have to re-create my widget(s) again. So to solve this issue, do I have to use .grid_remove() on every child widgets within my Frame widget(s)? Or is there any other advise/solution to this.
EDIT 1: To replicate the lagging behavior when dragging the app window, user need to click each button(s) to display each Frame widgets at least once.
I experienced the same behavior with my Tkinter app and I found out that it's caused by my mouse (SteelSeries Sensei Ten).
When I tried moving the window with a different generic mouse, the "slugish" movement was gone. Finally, I tried messing with my mouse settings and it turned out that lowering my polling rate from 1000 to 250 Hz fixes it as well.
I also found out that this behavior is dependent alongside the polling rate on the number of used widgets and the size of the window.
I guess that a better solution would be to limit the "refresh rate" of your application locally. However, I'm not sure if that's possible.
EDIT:
It is possible actually.
from time import sleep
def on_configure(e):
if e.widget == tk_root:
sleep(0.015)
if __name__ == '__main__':
tk_root = tk.Tk()
tk_root.bind("<Configure>", on_configure)
tk_root_width = 890
tk_root_height = 600
screen_width = tk_root.winfo_screenwidth()
screen_height = tk_root.winfo_screenheight()
x_coordinate = int((screen_width/2) - (tk_root_width/2))
y_coordinate = int((screen_height/2) - (tk_root_height/2))
tk_root.geometry("{}x{}+{}+{}".format(tk_root_width, tk_root_height, x_coordinate, y_coordinate))
app_gui = AppGui(tk_root)
tk_root.grid_columnconfigure(index = 0, weight = 1)
tk_root.grid_rowconfigure(index = 0, weight = 1)
app_gui.grid(column = 0, row = 0, columnspan = 1, rowspan = 1, padx = (1,1), pady = (1,1), sticky = 'nwse')
tk_root.mainloop()
This fixed it for me.
I'm trying to create an app that has 2 windows. The first window (A) is only called the first time the app is ran, and then opens window (B). For all future events, only window (B) is called. This is the following code:
# Script Counter/Setup
def read_counter():
if path.exists("counter.json"):
return loads(open("counter.json", "r").read()) + 1
else:
info = tk.Tk()
info.title("Setup")
info.geometry("350x120")
info.grid_columnconfigure((0, 2), weight = 1)
count = tk.Label(info, text = "Previous Post Number")
string = tk.StringVar()
count_input = tk.Entry(info, textvariable = string)
val = string.get()
def destroy_get():
val = int(count_input.get())
info.quit()
return val
count_button = tk.Button(info, text = "Done!", command = destroy_get)
tk.Label(info, text = "Setup", font='Helvetica 18 bold').grid(row = 0, column = 1, padx = 5, pady = 5)
count.grid(row = 1, column = 0, padx = 5, pady = 5)
count_input.grid(row = 1, column = 2, padx = 5, pady = 5)
count_button.grid(row = 2, column = 1, padx = 5, pady = 5)
info.mainloop()
# info.destroy()
return destroy_get()
def write_counter():
with open("counter.json", "w") as f:
f.write(dumps(counter))
counter = read_counter()
atexit.register(write_counter)
folders = ["to_post/", "not_posted/", "posted"]
for folder in folders:
if not path.exists(folder):
os.mkdir(folder)
print(os.getcwd())
# GUI
window = tk.Tk()
window.title("Confessions Bot")
window.geometry("600x350")
window.grid_columnconfigure((0,2), weight = 1)
label_tell_account = tk.Label(window, text = "Tellonym Account")
label_tell_password = tk.Label(window, text = "Tellonym Password")
label_ig_account = tk.Label(window, text = "Instagram Account")
label_ig_password = tk.Label(window, text = "Instagram Password")
tell_account = tk.Entry(window)
tell_password = tk.Entry(window)
ig_account = tk.Entry(window)
ig_password = tk.Entry(window)
image = ImageTk.PhotoImage(Image.open("logo.png"))
tk.Label(window, image = image).grid(row = 0, column = 1, padx = 10, pady = 10)
label_tell_account.grid(row = 1, column = 0)
tell_account.grid(row = 1, column = 2, padx = 10, pady = 10)
label_tell_password.grid(row = 2, column = 0, padx = 10, pady = 10)
tell_password.grid(row = 2, column = 2, padx = 10, pady = 10)
label_ig_account.grid(row = 3, column = 0, padx = 10, pady = 10)
ig_account.grid(row = 3, column = 2, padx = 10, pady = 10)
label_ig_password.grid(row = 4, column = 0, padx = 10, pady = 10)
ig_password.grid(row = 4, column = 2, padx = 10, pady = 10)
# run.grid(row = 5, column = 1, padx = 10, pady = 10)
window.mainloop()
When this is ran I get _tkinter.TclError: image "pyimage1" doesn't exist. I read that this happens since I haven't destroyed my initial window. If I change destroy_get() to have info.destroy() I'm no longer able to get the the entry from window A.
This is my first time using Tkinter and coding in general so any help is welcome.
Good afternoon,
I've been trying for two days to solve this problem and desperate, I am looking for your help. I want to show a plot (using matplotlib) within my tkinter application (not opening it in a different window) and the problem is that when I press any of the toolbar buttons as soon as I cross with the mouse coursor the lines of the plot, the plot disappears for a brief moment and appears again, but the toolbar buttons disappear until I cross them again with the mouse coursor. So for the button to appear back I should cross it and cross out off the button.
I noticed that if I change the background image (just the color, placed in label) the disappeared buttons are replaced with that color, so it seems that when I cross the lines of the plot everything in the Label except the plot is overlayed by the background.
Regards,
Wladyslaw
The code is:
import tkinter as tk
from tkinter.ttk import *
from tkinter import *
import tkinter.font as tkFont
from tkinter import scrolledtext
import logging
from PIL import Image, ImageTk
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import numpy as np
from matplotlib.figure import Figure
import numpy as np
import sys
if sys.version_info[0] < 3:
import Tkinter as tk
else:
import tkinter as tk
from matplotlib.backends import _backend_tk
from matplotlib.backends.backend_agg import FigureCanvasAgg
class WidgetLogger(logging.Handler):
def __init__(self, widget):
logging.Handler.__init__(self)
self.setLevel(logging.INFO)
self.widget = widget
self.widget.config(state='disabled')
def emit(self, record):
self.widget.config(state='normal')
# Append message (record) to the widget
self.widget.insert(tk.END, self.format(record) + '\n')
self.widget.see(tk.END) # Scroll to the bottom
self.widget.config(state='disabled')
class Product:
def __init__(self, window):
self.wind = window
self.wind.geometry("1060x700")
self.wind.title('Artificial intelligence based pricer')
self.wind.lift()
#self.wind.resizable(width=False, height=False)
background_color = '#526b70'
background_color2 = '#e4e8eb'
background_color3 = '#e4e8eb'
background_color4 = '#c4cfd2'
text_color = 'white'
style = ttk.Style()
style.configure("TProgressbar", background=background_color)
img = Image.open('Files/Images/background_image.jpg')
img = ImageTk.PhotoImage(img)
background = tk.Label(self.wind, image=img, bd=0)
background.grid(row = 0, column = 0, rowspan = 8, columnspan = 5)
background.image = img
img2 = Image.open('Files/Images/background_image2.jpg')
img2 = ImageTk.PhotoImage(img2)
background2 = tk.Label(self.wind, image=img2, bd=0)
background2.grid(row = 9, column = 0, rowspan = 10, columnspan = 10)
background2.image = img2
########## LEFT TOP SIDE ##############################
fontStyleTitle = tkFont.Font(family="Times New Roman (Times)", size=12, weight='bold')
fontStyleText = tkFont.Font(family="Arial", size=10, weight='bold')
Label1 = Label(background, text = ' ')
Label1.grid(row = 0, column = 0)
Label1.configure(background=background_color)
Label2 = Label(background, text = 'GLOBAL CONFIGURATION', fg=text_color, font=fontStyleTitle)
Label2.grid(row = 1, column = 1, padx = 3, columnspan = 2, sticky = W)
Label2.configure(background=background_color)
Label3 = Label(background, text = 'Shop ID: ', fg=text_color, font=fontStyleText)
Label3.grid(row = 2, column = 1, pady = 4, sticky = W)
Label3.configure(background=background_color)
self.shop = Entry(background)
self.shop.focus()
self.shop.grid(row = 2, column = 2)
self.shop.configure(background=background_color3)
Label4 = Label(background, text = 'Item ID: ', fg=text_color, font=fontStyleText)
Label4.grid(row = 3, column = 1, sticky = W)
Label4.configure(background=background_color)
self.item = Entry(background)
self.item.grid(row = 3, column = 2)
self.item.configure(background=background_color3)
Label5 = Label(background, text = '')
Label5.grid(row = 4, column = 1)
Label5.configure(background=background_color)
Label6 = Label(background, text = 'ANN CONFIGURATION', font = fontStyleTitle, fg=text_color)
Label6.grid(row = 5, column = 1, padx = 3, columnspan = 2, sticky = W)
Label6.configure(background=background_color)
Label7 = Label(background, text = 'Model: ', fg=text_color, font=fontStyleText)
Label7.grid(row = 6, column = 1, pady = 4, sticky = W)
Label7.configure(background=background_color)
self.model = Entry(background)
self.model.grid(row = 6, column = 2)
self.model.configure(background=background_color3)
Label8 = Label(background, text = 'Test set: ', fg=text_color, font=fontStyleText)
Label8.grid(row = 7, column = 1, sticky = W)
Label8.configure(background=background_color)
self.test_set = Entry(background)
self.test_set.grid(row = 7, column = 2)
self.test_set.configure(background=background_color3)
Button1 = tk.Button(background, bg=background_color2, text = 'Calculate performance')
Button1.grid(row = 8, column = 1, padx = 50, pady = 10, columnspan = 2, sticky = W+E)
#Button1.configure(background=background_color)
########## CENTER TOP SIDE ############################
Label9 = Label(background, text = 'ANN MODEL PERFORMANCE', font=fontStyleTitle, fg=text_color)
Label9.grid(row = 1, column = 3, padx = 40, sticky = W)
Label9.configure(background=background_color)
performace = Text(background, height=8, width=50)
performace.grid(row = 2, column = 3, padx = 40, rowspan = 6)
temporalText = '''MSE of standarized mean predictions: 700,5496
MSE of standarized ANN predictions: 700,5496
MSE of deseasonalized mean predictions: 700,5496
MSE of deseasonalized ANN predictions: 700,5496
MSE of seasonalized mean predictions: 700,5496
MSE of seasonalized ANN predictions: 700,5496'''
performace.insert(tk.END, temporalText)
performace.configure(background=background_color3)
Widget_Logger = WidgetLogger(performace)
progress = Progressbar(background, style='TProgressbar', orient = HORIZONTAL, length = 100, mode = 'determinate')
progress.grid(row = 8, column = 3, padx = 40, sticky = W+E)
#progress.configure(background=background_color)
########## RIGHT TOP SIDE #############################
Label10 = Label(background, text = ' ')
Label10.grid(row = 0, column = 6)
Label10.configure(background=background_color)
Label11 = Label(background, text = "PREDICTION'S CONFIGURATION", font=fontStyleTitle, fg=text_color)
Label11.grid(row = 1, column = 4, padx = 3, columnspan = 2, sticky = W)
Label11.configure(background=background_color)
Label12 = Label(background, text = 'Precision: ', fg=text_color, font=fontStyleText)
Label12.grid(row = 2, column = 4, pady = 4, sticky = W)
Label12.configure(background=background_color)
self.precision = Entry(background)
self.precision.focus()
self.precision.grid(row = 2, column = 5)
self.precision.configure(background=background_color3)
Label13 = Label(background, text = 'Max. price multiplicator: ', fg=text_color, font=fontStyleText)
Label13.grid(row = 3, column = 4, sticky = W)
Label13.configure(background=background_color)
self.max_price_multiplicator = Entry(background)
self.max_price_multiplicator.grid(row = 3, column = 5)
self.max_price_multiplicator.configure(background=background_color3)
Label14 = Label(background, text = 'Delta multiplicator: ', fg=text_color, font=fontStyleText)
Label14.grid(row = 4, column = 4, pady = 4, sticky = W)
Label14.configure(background=background_color)
self.delta_multiplicator = Entry(background)
self.delta_multiplicator.grid(row = 4, column = 5)
self.delta_multiplicator.configure(background=background_color3)
Label15 = Label(background, text = 'Item cost: ', fg=text_color, font=fontStyleText)
Label15.grid(row = 5, column = 4, sticky = W)
Label15.configure(background=background_color)
self.item_cost = Entry(background)
self.item_cost.grid(row = 5, column = 5)
self.item_cost.configure(background=background_color3)
Radiobutton(background, text = "absolute", variable = (background,"1"), value = "1", indicator = 0, background = "light blue", activebackground=background_color2, activeforeground='black').grid(row = 6, column = 4, sticky = E)
Radiobutton(background, text = "mean price multiplicator", variable = (background,"1"), value = "2", indicator = 0, background = "light blue", activebackground=background_color2, activeforeground='black').grid(row = 6, column = 5, pady = 4, sticky = W)
Label16 = Label(background, text = 'Fixed costs: ', fg=text_color, font=fontStyleText)
Label16.grid(row = 7, column = 4, sticky = W)
Label16.configure(background=background_color)
self.fixed_costs = Entry(background)
self.fixed_costs.grid(row = 7, column = 5)
self.fixed_costs.configure(background=background_color3)
Button2 = tk.Button(background, bg=background_color2, text = 'Calculate predictions')
Button2.grid(row = 8, column = 4, padx = 80, pady = 10, columnspan = 2, sticky = W+E)
Label17 = Label(background, text = ' ')
Label17.grid(row = 0, column = 6, sticky = W)
Label17.configure(background=background_color)
########## LEFT BOTTOM SIDE ###########################
Label18 = Label(background, text = ' ')
Label18.grid(row = 9, column = 1)
Label18.configure(background=background_color)
fig = Figure(figsize=(6, 4), dpi=100)
t = np.arange(0, 3, .01)
fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))
canvas = FigureCanvasTkAgg(fig, master=background2)
canvas.draw()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
toolbarFrame = Frame(master=background2)
toolbarFrame.pack(side=TOP, fill=BOTH, expand=1)
toolbar = NavigationToolbar2Tk(canvas, toolbarFrame)
toolbar.update()
canvas.mpl_connect("key_press_event", on_key_press)
def on_key_press(event):
print("you pressed {}".format(event.key))
key_press_handler(event, canvas, toolbar)
if __name__ == '__main__':
window = Tk()
application = Product(window)
window.mainloop()```
I'm trying to add items to the listbox, but every time I try it, it's saying that the global name "title_input" is not defined. I don't understand why this doesn't work, because the last one that I did with this exact same structure didn't give me that error. I'm new to this, and the other tutorials and questions I read about global name errors didn't make sense to me. Thanks for the help!
from Tkinter import *
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.create_widgets()
self.list = Listbox(self, selectmode=BROWSE)
self.list.grid(row = 1, column = 4, rowspan = 10, columnspan = 3, sticky = W, padx = 5, pady = 5)
def create_widgets(self):
#setlist box - may not stay text box?
self.setlist = Text(self, height = 14, width = 25)
self.setlist.grid(row = 1, column = 0, rowspan = 10, columnspan = 3, sticky = W, padx = 5, pady = 5)
self.setlistLabel = Label(self, text = "SetList")
self.setlistLabel.grid(row = 0, column = 1, sticky = W, padx = 5, pady = 5)
#Library label
self.libraryLabel = Label(self, text = "Library")
self.libraryLabel.grid(row = 0, column = 5, sticky = W, padx = 5, pady y = 5)
#Library button/input
self.add_title = Button(self, text = "Add Title", command = self.add_item)
self.add_title.grid(row = 16, column = 5, sticky = W, padx = 5, pady = 5)
self.title_input = Entry(self)
self.title_input.grid(row = 16, column = 4, sticky = W, padx = 5, pady = 5)
def add_item(self):
list.insert(END, title_input.get())
def get_list(event):
index = list.curselection()[0]
seltext = list.get(index)
setlist.insert(0, seltext)
root = Tk()
root.title("SetList Creator")
root.geometry("500x500")
app = Application (root)
root.mainloop()
title_input is defined in your instance namespace but it is not defined in your global namespace. When you reference an unqualified title_input in your class method add_item, Python looks for title_input in the global namespace. When it doesn't find it there it gives you that error. Adding the self qualifier, self.title_input, to indicate that you wish to reference title_input in the instance namespace, will resolve the error.