在多个python脚本上从不同的函数构造Tkinter GUI

2024-09-30 04:33:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想构建用于观察结果的GUI。但是,由于GUI包含多个模块,每个模块都包含多个小部件,我还想在将来添加额外的函数,所以我为每个模块构造了多个python脚本。主模块(称为test.py)如下所示:

import tkinter as Tk
from tkinter import*
import test2
class test:
    def __init__(self,master):
        button = Button(gui, text = 'click here', command = self.printtestcommand)
        button.pack()
gui = Tk()
a = test(gui)
gui.mainloop()

命令printtestcommand位于另一个脚本(test2.py)上:

def printtestcommand(self):
    print('Test completed')

但是当我运行test.py脚本时,遇到了以下错误:

AttributeError: 'test' object has no attribute 'printtestcommand'

有人能告诉我我哪里出了问题,以及如何解决这个问题吗


Tags: 模块pytestimportself脚本部件tkinter

热门问题