简体   繁体   English

Python - Tkinter 管理框架

[英]Python - Tkinter Manage Frames

I have a small script which is organized in 3 frames:我有一个小脚本,分为 3 个框架:

  • 1 in the first row 1 在第一行
  • 1 in the second row Left 1 在第二行左
  • 1 in the second row right 1 在第二行右边

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.我按下第一行框架中的按钮,将输入值交给左侧第二行的 Label。

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.您的程序无法正常工作的原因是,在MainLeftFrameMainRightFrame上使用forget后,当调用action function 时,您不会再次打包它们。 Adding these 2 lines of code in action function should make it work.action中添加这两行代码应该可以使它工作。 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.这不是唯一的问题,每次调用 function 时定义新的小部件并打包它们会一遍又一遍地增加相同的小部件集。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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