Tkinter按钮 - 导入和运行模块

时间:2017-09-20 13:37:40

标签: python tkinter tk

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

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

public class SearchResponse extends ResourceSupport {...}

带有tkinter按钮的模块:

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

2 个答案:

答案 0 :(得分:1)

from program import DoSomething

在该按钮的命令上,您只需拨打def DoSomething()

即可

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

答案 1 :(得分:1)

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

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;单击该按钮时,将调用此命令。