python - 为什么通过从另一个模块调用函数来显示来自不同模块中不同 GUI 的图像时会出现问题?

标签 python tkinter error-handling

我试图制作一个模块,在其中我制作了一个功能,它只是在 GUI 中读取和显示图像。然后我制作了另一个模块,在单击按钮时调用该函数。按钮给我错误。

#module code:
from tkinter import *
class disp:
    def __init__(self):
        root1.geometry("400x500")
        image = PhotoImage(file = 'png2.png')
        Label(root1,image=image).pack()
        root1.mainloop()


#main code:
from tkinter import *
import testimg as ti

def click():
    ti.disp()

root = Tk()

Button(text = 'Click me',command=click).pack()
root.mainloop()

最佳答案

在您的类 disp 中,您将 master 设置为 root1,而在主代码中,您将 Tk() 定义为 root。这意味着 root1 不是窗口,因此拥有 root1 主控的标签没有自己打包的地方。

您还需要删除 root1.mainloop(),因为它没有用,并且由于 root1 没有 Tk() 而导致错误。这就像试图在不输入 while 的情况下循环一个 while 语句。这给出了一个错误。

关于python - 为什么通过从另一个模块调用函数来显示来自不同模块中不同 GUI 的图像时会出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60038130/

相关文章:

python - 如何模拟在 __init__ 中实例化的类属性?

python - 文本小部件中是否有用于加入撤消/重做堆栈的 Python Tkinter 方法?

error-handling - glmer 模型 list2env(数据) : first argument must be a named list 中出现错误

python - 我如何证明某些数据来 self 的应用程序?

python - SQLAlchemy 按引用关系的混合属性排序

python - Json 文件中的列表框

grid - 通过网格在Tkinter中显示Matplotlib导航工具栏

error-handling - 服务器的响应缺少必填字段时,是否可以使用状态代码?

c++ - 无法将 ‘int (*)[m]’转换为‘int(*)[100000]

python - 在python中将SVG转换为灰度