0

I am trying to make this code to later integrate with some structures calculations.

I want to delete all widgets in the root when I press Section Properties. And then I want to write new widgets but outside of the function. I can't get it to work.

from tkinter import *
from PIL import Image, ImageTk


#set up the environment - root
root = Tk()
root.title('Statics Calculator - Menu')
root.geometry("500x500") 
root.iconbitmap(r'C:\Users\Jaume\OneDrive\JAUME\SES MEVES COSES\Nabla yacht design.ico')

#welcome user
welcome = Label(root, text='Welcome to Statics Calculator', font=(20)).pack()
mode = Label(root, text='How Can I Help You?', font=(20)).pack()

#background image
back_img = ImageTk.PhotoImage(Image.open('Golden bridge1.png'))
back_image = Label(image=back_img)
back_image.place(x=0, y=110, relwidth=1)

def opensections():
    for widget in root.winfo_children():
        widget.destroy()
        
    root.title("Statics Calculator - Section Properties")
    global sections_frame
    sections_frame = Frame(root, width=500, height = 00).pack()


sections = Button(root, text='Section Properties', command=opensections).pack()
sections_module_label = Label(sections_frame, text ="You Are Now In The Section Properties Module", font=10).pack(side=TOP) 

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Why do you want to write new widgets, outside of the function – Delrius Euphoria Mar 01 '21 at 20:39
  • Why can't you get it to work? What does the code you posted do? What do you mean by creating widgets outside of the function? They will have to be in a function somewhere. – Bryan Oakley Mar 01 '21 at 20:46
  • maybe put all widgets in one `Frame` and then you will have to destroy only this `Frame`. OR maybe put on list all widgets which you want to remove later. – furas Mar 01 '21 at 21:21
  • you have the most common mistake - `variable = Widget().pack()` - it assings `None` to variable because `pack()` always returns `None`. You have to do it in two steps: first: `variable = Widget()` and later `variable.pack()` – furas Mar 01 '21 at 21:24
  • it is good rule to put all `global` at the beginning of function and put all functions directly after imports - before `root = Tk()`. This makes code more readable. See more: [PEP 8 -- Style Guide for Python Code](https://www.python.org/dev/pps/pep-0008/) – furas Mar 01 '21 at 21:29
  • if you want create new widgets after pressing button then you will have to create them INSIDE some function - so you can do it inside `opensections()`, or in other function but executed in `opensections()` – furas Mar 01 '21 at 21:32
  • I agree with @furas about putting the groups of widgets on separate `Frame`s and switching between them. See Bryan Oakley's answer to [Switch between two frames in tkinter](https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter) – martineau Mar 01 '21 at 22:22
  • Thank you very much to everyone. Using all of your tricks I managed to make it work. – Jaume Triay Mar 02 '21 at 22:40

0 Answers0