【发布时间】:2020-10-27 12:12:06
【问题描述】:
制作了一个名为Mainpage 的tkinter 窗口,其中的sn-p 出现在下面:
def mainpage():
import tkinter as tk
from PIL import ImageTk, Image
root = tk.Tk()
image=Image.open("logo2.png")
photo = ImageTk.PhotoImage(image)
lab = tk.Label(root, image=photo, bd=0, highlightthickness=0) # <----Error is coming for this line in the next code
lab.image=photo
lab.place(x=460,y=245)
button = tk.Button(root, text="Search",font="CenturyGothic",fg="Darkred",width="70",
height="24",command=veryuseful)
img1 = ImageTk.PhotoImage(file="gold.png")
button.config(image=img1,compound="center")
button.place(x=850,y=340)
root.mainloop()
代码中没有任何问题。 但是,当我将此文件导入另一个代码时,会发生错误:
import Mainpage # <----imported the file here
condition=0
for i in d:
if i[3]==username:
if i[4]==password:
head=tk.Label(win, text="Welcome to Library X",fg="Light blue",bg="Black" ,font=("Arial",12))
head.grid(column=1, row=7)
condition=1
break
else:
messagebox.showerror("ERROR", "Wrong Password!")
condition=2
break
else:
continue
condition=0
if condition==0:
messagebox.showerror("ERROR", "Wrong Username!")
if condition==1:
Mainpage.mainpage() # <------Used the function mainpage from file
一个名为“pyimage”的错误我已经多次看到该错误但这次很奇怪
TCL 错误为:"pyimage10" doesn't exist
当编辑再次尝试时,它会更改为“pyimage16”,然后继续将 6 添加到编号中。在pyimage旁边...
如果您知道这个问题的解决方案,我将永远感激不尽。 我已经试过了:
- 提供了文件的完整路径
- 使用了对图片的引用
- 重新启动内核
正如我所说,当我运行主页文件时它可以正常工作,但是......当我将它导入另一个文件时,问题出在 tkinter 中。
【问题讨论】:
-
在导入
Mainpage的代码中,是否也创建了Tk的实例?您所描述的是当您创建多个Tk实例时出现的问题之一。 -
mainpage()定义的缩进是否正确?看起来很可疑。 -
一般来说,我不希望在整个程序中多次调用
mainloop()方法。 -
Bryan Oakley 你能告诉我如何在不添加 Tk() 实例的情况下添加多个小部件吗?我是初学者,请告诉我...
-
Donal Fellows,缩进是对的,只是我在这里复制时可能会出现一两个错误,并且我只使用过一次mainloop()......朋友还有其他建议吗?
标签: image tkinter photoimage