将 Frame 从原始 window 更改为另一个
Changing a Frame from an original window to another
我有这个函数,每次调用另一个函数时都会生成一个按钮,并将新按钮放入新的 window。我已经成功地将这段代码实现到我的主程序中,除了一个部分,按钮不会在除我的主 window 之外的任何其他 window 中生成。代码有点长,请见谅
import tkinter as tk
from pathlib import Path
def window2():
editor = tk.Toplevel()
editor.title("Window 2")
def add_buttons(*items):
for item in items:
auto_button = tk.Button(button_frame, text=item)
auto_button.grid(column=1)
def get_items(path):
if path.exists():
with open(path, "r") as f:
data = f.read().strip().splitlines()
return data
return []
def save_items(path, items):
with open(path, "w") as f:
f.write("\n".join(items))
def submit():
global items
# get the text and append it to our global list of button text
text = entry.get()
items.append(text)
# create the button
add_buttons(text)
# save the list of items
save_items(path, items)
root = tk.Tk()
button_frame = tk.Frame(root)
entry = tk.Entry(root)
submit = tk.Button(root, text="Submit", command=submit)
entry.grid(row=0, column=1)
submit.grid(row=1, column=0, columnspan=2)
button_frame.grid(column=1)
window2_btn = tk.Button(root, text="Take me to window 2", command=window2).grid(row=2, column=0,)
# initialize our list of buttons from a file
path = Path("buttons.txt")
items = get_items(path)
add_buttons(*items)
root.mainloop()
我现在已经在 3 个不同的程序中对此进行了测试,所有程序都具有相同的按钮模式,可以在主启动 window 中正常运行,但不允许我调用该函数以在主启动中运行与上文 button_frame = tk.Frame(window) 不同的 window。上面的代码几乎是 Bryan Oakley 的原汁原味,归功于他,我回到他发给我的代码示例中,以显示我的最小值,但有同样的错误。每次调用这个函数时,它都会给出一个未定义的(window 在这个例子中)错误。我知道这不是一个硬性解决方案,但看在我的份上,我一直无法弄清楚。
这是完整的脚本。此代码有效!
from pathlib import Path
import tkinter as tk
def window2():
window.deiconify()
window.lift()
window.title("Window 2")
window.focus_force()
def add_buttons(*items):
for item in items:
auto_button = tk.Button(button_frame, text=item)
auto_button.grid(column=1)
def get_items(path):
if path.exists():
with open(path, "r") as f:
data = f.read().strip().splitlines()
return data
return []
def save_items(path, items):
with open(path, "w") as f:
f.write("\n".join(items))
def submit():
global items
# get the text and append it to our global list of button text
text = entry.get()
items.append(text)
# create the button
add_buttons(text)
# save the list of items
save_items(path, items)
root = tk.Tk()
window = tk.Toplevel()
button_frame = tk.Frame(window)
entry = tk.Entry(root)
submit = tk.Button(root, text="Submit", command=submit)
entry.grid(row=0, column=1)
submit.grid(row=1, column=0, columnspan=2)
button_frame.grid(column=1)
window2_btn = tk.Button(root, text="Take me to window 2", command=window2).grid(row=2, column=0,)
# initialize our list of buttons from a file
path = Path("buttons.txt")
items = get_items(path)
add_buttons(*items)
root.mainloop()
我有这个函数,每次调用另一个函数时都会生成一个按钮,并将新按钮放入新的 window。我已经成功地将这段代码实现到我的主程序中,除了一个部分,按钮不会在除我的主 window 之外的任何其他 window 中生成。代码有点长,请见谅
import tkinter as tk
from pathlib import Path
def window2():
editor = tk.Toplevel()
editor.title("Window 2")
def add_buttons(*items):
for item in items:
auto_button = tk.Button(button_frame, text=item)
auto_button.grid(column=1)
def get_items(path):
if path.exists():
with open(path, "r") as f:
data = f.read().strip().splitlines()
return data
return []
def save_items(path, items):
with open(path, "w") as f:
f.write("\n".join(items))
def submit():
global items
# get the text and append it to our global list of button text
text = entry.get()
items.append(text)
# create the button
add_buttons(text)
# save the list of items
save_items(path, items)
root = tk.Tk()
button_frame = tk.Frame(root)
entry = tk.Entry(root)
submit = tk.Button(root, text="Submit", command=submit)
entry.grid(row=0, column=1)
submit.grid(row=1, column=0, columnspan=2)
button_frame.grid(column=1)
window2_btn = tk.Button(root, text="Take me to window 2", command=window2).grid(row=2, column=0,)
# initialize our list of buttons from a file
path = Path("buttons.txt")
items = get_items(path)
add_buttons(*items)
root.mainloop()
我现在已经在 3 个不同的程序中对此进行了测试,所有程序都具有相同的按钮模式,可以在主启动 window 中正常运行,但不允许我调用该函数以在主启动中运行与上文 button_frame = tk.Frame(window) 不同的 window。上面的代码几乎是 Bryan Oakley 的原汁原味,归功于他,我回到他发给我的代码示例中,以显示我的最小值,但有同样的错误。每次调用这个函数时,它都会给出一个未定义的(window 在这个例子中)错误。我知道这不是一个硬性解决方案,但看在我的份上,我一直无法弄清楚。
这是完整的脚本。此代码有效!
from pathlib import Path
import tkinter as tk
def window2():
window.deiconify()
window.lift()
window.title("Window 2")
window.focus_force()
def add_buttons(*items):
for item in items:
auto_button = tk.Button(button_frame, text=item)
auto_button.grid(column=1)
def get_items(path):
if path.exists():
with open(path, "r") as f:
data = f.read().strip().splitlines()
return data
return []
def save_items(path, items):
with open(path, "w") as f:
f.write("\n".join(items))
def submit():
global items
# get the text and append it to our global list of button text
text = entry.get()
items.append(text)
# create the button
add_buttons(text)
# save the list of items
save_items(path, items)
root = tk.Tk()
window = tk.Toplevel()
button_frame = tk.Frame(window)
entry = tk.Entry(root)
submit = tk.Button(root, text="Submit", command=submit)
entry.grid(row=0, column=1)
submit.grid(row=1, column=0, columnspan=2)
button_frame.grid(column=1)
window2_btn = tk.Button(root, text="Take me to window 2", command=window2).grid(row=2, column=0,)
# initialize our list of buttons from a file
path = Path("buttons.txt")
items = get_items(path)
add_buttons(*items)
root.mainloop()