0

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.

L.C
  • 47
  • 1
  • 1
  • 12
  • 2
    Try `b1 = Button(root, text='File Cleaner', height=5, command=file_cleanup)` instead of `b1 = Button(root, text='File Cleaner', height=5, command=file_cleanup())`. Also you're trying to call `b1.pack_forget()` in `b1` assignment. This won't work – Andersson Jan 04 '17 at 14:37
  • @Andersson them pesky brackets and auto complete, thanks that's solved one issue. Any idea on the pack.forget situation – L.C Jan 04 '17 at 14:42
  • 1
    `var.trace('w', change_layout())` here also. – Lafexlos Jan 04 '17 at 14:44
  • @Lafexlos I've changed that but it hasn't really done anything, I have noticed if i click file cleaner again on the page it should be hidden it duplicates the cleanchat and clean lex buttons – L.C Jan 04 '17 at 14:49
  • @LC, you can try to use `lift()` and `lower()` methods to switch buttons visibility – Andersson Jan 04 '17 at 14:50
  • @Andersson I've never worked with that before is there a webpage or example you could give for what you mean. Do you mean it's the same as pack and pack_forget in hiding the buttons. Thanks – L.C Jan 04 '17 at 14:53
  • 1
    http://stackoverflow.com/q/5767228/7432 – Bryan Oakley Jan 04 '17 at 14:56
  • @BryanOakley Thanks for that read, but i can't see how that helps me with my main issue which is the pack_forget error and to overcome that. – L.C Jan 04 '17 at 15:03
  • 1
    You get the "pack_forget" error because it is being called before b1 has been created, due to the fact you're calling `file_cleanup()` before assigning it to the `command` attribute. – Bryan Oakley Jan 04 '17 at 15:05
  • Thank you everyone for the great comments my issue seems to be resolved, I still have more to work through but as they aren't to do with posted thread I'll leave them till next time – L.C Jan 04 '17 at 15:07

0 Answers0