【发布时间】:2021-07-07 12:49:05
【问题描述】:
我正在尝试创建一个顶层窗口,但是,这个顶层窗口是从函数内同一目录中的不同文件调用的。
抱歉,我绝不是 tkinter 或 python 大师。这是代码的两部分。 (sn-ps)
#文件 1(主要)
import tkinter as tk
from tkinter import *
import comm1
from comm1 import com1
root = tk.Tk()
root.title("")
root.geometry("1900x1314")
#grid Center && 3x6 configuration for correct gui layout
root.grid_rowconfigure(0, weight=1)
root.grid_rowconfigure(11, weight=1)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(11, weight=1)
#background image
canvas = Canvas(root, width=1900, height=1314)
canvas.place(x=0, y=0, relwidth=1, relheight=1)
bckground = PhotoImage(file='img.png')
canvas.create_image(20 ,20 ,anchor=NW, image=bckground)
#command to create new Toplevel
btn1 = tk.Button(root, text='Top', command=com1, justify='center', font=("Arial", 10))
btn1.config(anchor=CENTER)
btn1.grid(row=4, column=1)
#文件 2(顶层)
#command for new window
def com1():
newWindow1 = Toplevel(root)
newWindow1.title("")
newWindow1.geometry("500x500")
entry1 = tk.Entry(root, justify='center' , font=("Arial", 12), fg="Grey")
newWindow1.pack()
newWindow1.mainloop()
奇怪的部分是它完美地运行了几分钟,并且没有更改任何代码它只是停止工作。 我哪里错了?
【问题讨论】:
-
newWindow1.pack()应该是entry1.pack()。 -
我认为你应该在调用函数时传入
root,因为现在root没有定义。也代替entry1 = tk.Entry(root, ...)使用entry1 = tk.Entry(newWindow1, ...) -
哦,谢谢您的反馈!是的,这绝对是一个错误。