Tkinter 按钮 - 导入和 运行 模块

Tkinter button - import and run module

我想 运行 一个 python 程序(例如:program.py),当在一个单独的程序中单击 tkinter window 中的按钮时。但是,当我从模块导入 class 时,它是 运行s。单击时如何获取 运行 模块的按钮?非常感谢任何帮助。

运行 (program.py) 的其他模块:

class sampleProgram():
    def DoSomething():
        print('Do Something')

带有 tkinter 按钮的模块:

from program import DoSomething

class Window(Frame)
    def __init__(self,master = None):
        <Stuff In Window>

    def addWidgets(self):
        <Widgets To Add>

    def init_window(self):
        self.pack(fill=BOTH, expand=1)
        RunButton = Button(self, text="Run", command=<**What Goes Here To Run sampleProgram?**>)

from program import DoSomething

在该按钮的命令中,您只需调用 def DoSomething()

RunButton = Button(self, text="Run", command=DoSomething)

您必须在 sampleProgram class 上致电 DoSomething;为此,您必须导入它。

Class sampleProgram():
    def DoSomething():              # <--- this is a staticmethod
        print('Do Something')

带有 tkinter 按钮的模块:

from program import sampleProgram   # <--- import the class sampleProgram

Class Window(Frame)
    def __init__(self,master = None):
        <Stuff In Window>

    def addWidgets(self):
        <Widgets To Add>

    def init_window(self):
        self.pack(fill=BOTH, expand=1)
        RunButton = Button(self, text="Run", command=sampleProgram.DoDomething)

您将 sampleProgram.DoDomething 静态方法绑定到 runButton command;单击按钮时,将调用此命令。