英文:
Use of widget.pack() does not recover forgotten pack widget in Tkinter
问题
Here's the translated code snippet you provided:
import tkinter
top = tkinter.Tk()
top.geometry("800x800")
top.title("Text Editor")
edit = False
name = tkinter.Entry(top)
def editTrue():
global edit
edit = True
name.pack_forget()
def editFalse():
global edit
edit = False
name.pack()
menubar = tkinter.Menu(top)
filemenu = tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=editFalse)
filemenu.add_command(label="Edit", command=editTrue)
menubar.add_cascade(label="File", menu=filemenu)
top.config(menu=menubar)
text = tkinter.Text(top, height=800, width=800)
def saveNotes():
notes = text.get(1.0, "end")
f = open(name.get() + ".txt", "a")
f.write(notes)
f.close()
button = tkinter.Button(top, text="Save", command=saveNotes)
button.pack()
name.pack()
text.pack()
top.mainloop()
Regarding your issue with widget.pack_forget()
and widget.pack()
, make sure to use the global
keyword inside the editTrue()
and editFalse()
functions to modify the global variable edit
, as you are currently creating a local edit
variable within those functions.
英文:
import tkinter
top = tkinter.Tk()
top.geometry("800x800")
top.title("Text Editor")
edit = False
name = tkinter.Entry(top)
def editTrue():
edit = True
name.pack_forget()
def editFalse():
edit = False
name.pack()
menubar = tkinter.Menu(top)
filemenu = tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=editFalse)
filemenu.add_command(label="Edit", command=editTrue)
menubar.add_cascade(label="File", menu=filemenu)
top.config(menu=menubar)
text = tkinter.Text(top, height=800, width=800)
def saveNotes():
notes = text.get(1.0, "end")
f = open(name.get() + ".txt", "a")
f.write(notes)
f.close()
button = tkinter.Button(top, text="Save", command=saveNotes)
button.pack()
name.pack()
text.pack()
top.mainloop()
This is my code so far, and the problem is, when I call the widget.pack_forget()
function, it works perfectly well. It hides it and all that stuff. But, when I call the widget.pack()
again to recover it, it doesn't come up. I don't understand why. I'd appreciate some help.
答案1
得分: 1
你的文本框太大,宽度为800个字符,高度为800行。所以当 name
被 移除 (name.pack_forget()
) 然后再次 显示 (name.pack()
) 时,它会被打包在文本框下方。由于文本框太大,name
被推出了窗口的可视区域。
要么将文本框变小 (width=80, height=40
),要么在文本框之前 pack name
,使用 name.pack(before=text)
。
英文:
Your text box is too big, 800 characters width and 800 lines height. So when name
is removed (name.pack_forget()
) and then shown again (name.pack()
), it will be packed below the text box. Since the text box is too big, name
is pushed out of the viewable area of the window.
Either making the text box smaller (width=80, height=40
), or pack name
before the text box using name.pack(before=text)
.
答案2
得分: 1
当我再次调用widget.pack()以恢复它时,它不会出现。请使用lambda
代替创建的函数。
- 在第13行,在command关键字中添加
lambda
和pady
。 - 在第14行,在command关键字中添加
lambda
。 - 在第32行,在
Entry
小部件中添加pady=10
。
代码片段:
import tkinter
top = tkinter.Tk()
top.geometry("800x800")
top.title("Text Editor")
edit = False
name = tkinter.Entry(top)
menubar = tkinter.Menu(top)
filemenu = tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=lambda: name.pack(pady=10))
filemenu.add_command(label="Edit", command=lambda: name.pack_forget())
menubar.add_cascade(label="File", menu=filemenu)
top.config(menu=menubar)
text = tkinter.Text(top, height=40, width=80)
def saveNotes():
notes = text.get(1.0, "end")
f = open(name.get() + ".txt", "a")
f.write(notes)
f.close()
button = tkinter.Button(top, text="Save", command=saveNotes)
button.pack()
text.pack()
name.pack(pady=10)
top.mainloop()
截图:您将看到Entry小部件在底部隐藏和显示。
英文:
> when I call the widget.pack() again to recover it, it doesn't come up
Use lambda
instead of created functions.
- In line 13, add
lambda
andpady
in command keyword. - In line 14,
lambda
in command keyword. - In line 32, Add pady=10 in
Entry
widget.
Snippet:
import tkinter
top = tkinter.Tk()
top.geometry("800x800")
top.title("Text Editor")
edit = False
name = tkinter.Entry(top)
menubar = tkinter.Menu(top)
filemenu = tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=lambda: name.pack(pady=10))
filemenu.add_command(label="Edit", command=lambda: name.pack_forget())
menubar.add_cascade(label="File", menu=filemenu)
top.config(menu=menubar)
text = tkinter.Text(top, height=40, width=80)
def saveNotes():
notes = text.get(1.0, "end")
f = open(name.get() + ".txt", "a")
f.write(notes)
f.close()
button = tkinter.Button(top, text="Save", command=saveNotes)
button.pack()
text.pack()
name.pack(pady=10)
top.mainloop()
Screenshot: You will see Entry widget hidden and shown at the bottom.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论