I have 5 buttons total so far, I'm trying to display three of them on my homepage hide the other two then when i select my button called 'file_cleanup' Buttons 1, 2 & 3 are supposed to hide and buttons 4 & 5 appear. I think i understand why my error occurs but i don't know or can find a way around it.
code:
from tkinter import *
from tkinter.filedialog import askopenfilenames
from main_code import *
import sys
OPTIONS = [
"Homepage",
"Instructions"
]
root = Tk()
root.title('Title')
root.geometry('700x300')
var = StringVar(root)
var.set = ('Menu')
menu = OptionMenu(root, var, *OPTIONS)
menu.pack(side=TOP, anchor=W)
separator = Frame(height=2, bd=1, relief=SUNKEN)
separator.pack(fill=X, padx=1, pady=20)
top = Frame(root)
center = Frame(root)
bottom = Frame(root)
top.pack(side=TOP)
center.pack(side=TOP)
bottom.pack(side=BOTTOM, fill=BOTH, expand=True)
#Method changes GUI when an option from menu is selected
def change_layout(*args):
if var.get() == "Homepage":
b1.pack(in_=center, side=LEFT)
b2.pack(in_=center, side=LEFT)
b3.pack(in_=center, side=LEFT)
b4.pack_forget()
b5.pack_forget()
if var.get() == "Instructions":
b1.pack_forget()
b2.pack_forget()
b3.pack_forget()
def load_file():
fname = askopenfilenames(filetypes= (("Text Files", ".txt"),
("HTML Files", "*.html;*.htm"),
("All Files", "*.*")))
if fname:
try:
print('Files loaded')
except OSError as Error:
print('Files encountered error while loading')
def file_cleanup():
b1.pack_forget()
b2.pack_forget()
b3.pack_forget()
b4 = Button(root, text='Clean chat file', height=5, command=load_file)
b5 = Button(root, text='Clean lex file', height=5, command=load_file)
b4.pack(in_=center, side=LEFT)
b5.pack(in_=center, side=LEFT)
var.trace('w', change_layout)
# widgets for the top part of the GUI
b1 = Button(root, text='File Cleaner', height=5, command=file_cleanup)
b1.place(x=170, y=500)
b2 = Button(root, text='Run Program', height=5)
b3 = Button(root, text='Save Results', height=5)
b1.pack(in_=center, side=LEFT)
b2.pack(in_=center, side=LEFT)
b3.pack(in_=center, side=LEFT)
b4 = Button(root, text='Clean chat file', height=5, command=load_file)
b5 = Button(root, text='Clean lex file', height=5, command=load_file)
#Instructions - TODO Later date
root.mainloop()
Error Message:
Traceback (most recent call last):
File "C:/Users/Lewis Collins/PycharmProjects/program_w_gui/Home.py", line 74, in <module>
b1 = Button(root, text='File Cleaner', height=5, command=file_cleanup())
File "C:/Users/Lewis Collins/PycharmProjects/program_w_gui/Home.py", line 65, in file_cleanup
b1.pack_forget()
NameError: name 'b1' is not defined
if you're wondering why i declare b4 & b5 twice it is because file_cleanup fires back an error if i don't declare them inside there as well. This is my first time of using Tkinter in 3.5, the differences in error's I'm receiving compared to similar work I've done with tkinter on 2.7 is really slowing my work rate.
All help and feedback is much appreciated, Thanks in advance.