简体   繁体   中英

Python - Tkinter Manage Frames

I have a small script which is organized in 3 frames:

  • 1 in the first row
  • 1 in the second row Left
  • 1 in the second row right

I press the button in the first row frame and hand over the input value to the Label in the second row in the left.

Here my code:

import tkinter as tk

# Create Window
root = tk.Tk()

# Define String Variable
Name = tk.StringVar()

# Organize root window in 3 frames
EntryFrame = tk.Frame(root)
MainLeftFrame = tk.Frame(root)
MainRightFrame = tk.Frame(root)

# Create Buttons, Entry and Labels

NameLabel = tk.Label(MainLeftFrame, textvariable=Name)
InputName = tk.Entry(EntryFrame, width=20,bg='yellow')
SubmitButton = tk.Button(EntryFrame, text='Submit', command=lambda:action())

# Define what happens when press button reset
def reset():
    MainLeftFrame.forget()
    MainRightFrame.forget()
    EntryFrame.pack()

# Define what happens when button is pressed
def action():
    Name.set(InputName.get())
    ResetButton = tk.Button(MainRightFrame, text='Reset', command=lambda: reset())
    ResetButton.pack()
    Placeholder = tk.Label(MainRightFrame, text="place holder")
    Placeholder.pack(side="top")
    EntryFrame.forget()

# Pack Widgets
EntryFrame.pack(side='top')
MainLeftFrame.pack(side='left')
MainRightFrame.pack(side='right')

InputName.pack()
SubmitButton.pack()
NameLabel.pack()

#mainloop
root.mainloop()

Now to my question: When I press the "Submit" Button for the Second time (after pressing Reset Button) nothing is happening:(

Thanks in advance!

The reason of your program not working is that, after using forget on the MainLeftFrame and MainRightFrame you aren't packing them again when the action function is called. Adding these 2 lines of code in action function should make it work. BUT

MainLeftFrame.pack()
MainRightFrame.pack()

That's not the only issue, defining new widgets every time the function is called and packing them will additively increase the same widget set over and over again. To avoid this, you would have to predefine them and them perform forget and repacking. But a better thing to do would be to have a dedicated frame for them, so that it becomes easy for you to toggle. I have tried rewriting your script, let me know if this is what you wanted.

from tkinter import *

def reset():
    entry_frame.pack()
    main_frame.pack_forget()

def submit():
    entry_frame.pack_forget()
    main_frame.pack()
    name.set(name_entry.get())

root=Tk()

entry_frame=Frame(root)
entry_frame.pack()
name_entry=Entry(entry_frame)
name_entry.pack(side='top')
submit_button=Button(entry_frame,text='Submit',command=submit)
submit_button.pack(side='top')

main_frame=Frame(root)
reset_button=Button(main_frame,text='Reset',command=reset)
reset_button.pack(side='top')
name=StringVar()
name_label=Label(main_frame,textvariable=name)
name_label.pack(side='left')
placeholer_label=Label(main_frame,text='placeholer')
placeholer_label.pack(side='right')

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM