【问题标题】:Python 2.7 Tkinter - Opening GUI windows from a Helper filePython 2.7 Tkinter - 从帮助文件打开 GUI 窗口
【发布时间】:2015-02-11 00:10:52
【问题描述】:

我有 2 个文件。 GUI.py 和 Helper.py。 我将所有逻辑都放在 Helper.py 中

在 Helper.py 中我导入了 GUI,因为我想在 Helper 中发生某些事情时从 GUI 调用一个窗口。到目前为止,这是一个好方法吗?

现在在 GUI.py 我有类 GUI(Frame) 和 init(self, parent)

我要调用的函数在 init ..那么我该如何从帮助文件中调用它呢?

到目前为止,我在 Helper.py 中尝试过这个 导入界面 GUI = GUI.GUI()

但我得到错误: TypeError: init() 正好接受 2 个参数(1 个给定)

我做错了什么?

我试图把 self 放在那里,但我的 my_class 实例没有属性 'tk'

【问题讨论】:

  • 那么您的init 方法中的parent 是什么? Tkinter.Tk() ?
  • 说实话不太确定。在上面我有 from Tkinter import * 然后我有类 GUI(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent

标签: python user-interface instance parent self


【解决方案1】:

我认为你的代码是这样的:

GUI.py

class GUI(Frame):
    def __init__(self,parent):
        Frame.__init__(self,parent)
        self.parent = parent
        self.addQuitBtn()
        self.pack()
        mainloop()

    def addQuitBtn(self):
        quit = Button(self.parent,text='QUIT',command=self.parent.quit,activeforeground='white',activebackground='red')
        quit.pack()

    def beautifulChartWindow(self):
        pass

所以__init__ 方法中的parent 只是 Tk 的一个对象,如果你想创建 GUI 并在 Helper.py 等其他 python 脚本中显示它,请使用以下代码:

Helper.py

import GUI
from Tkinter import *

root = Tk()
root.geometry('600x400')

#What you have been stucked is how to create the GUI object.Just do it like this
#GUI = GUI.GUI(root)

#Once you got the GUI object, you can call the methods in GUI.py like this(here in file Helper.py)

#So you need to pass the GUI object that you have created before(Maybe just in the file GUI.py? You should call this method like Helper.myBeautifulChartWindow(GUI).This GUI is the object GUI,not the module GUI nor class GUI)
def myBeautifulChartWindow(MyGUIObject):
    MyGUIObject.beautifulChartWindow()

【讨论】:

  • 我想你误解了我一点。我想从 Helper.py 调用一个窗口到 GUI.py。换句话说,我想做这样的事情:在 Helper 中使用 GUI.beautifulChartWindow(),然后在 GUI.py 中打开它
  • GUI.py 是我保存所有 GUI 东西的地方,Helper 是我所有的逻辑,但我希望基本上如果“逻辑”发生某些事情,那么它会在 GUI 中调用一个窗口.
  • @Richard 我想我没有误会。请再看我的帖子。我刚刚修改了它。
  • 当我这样做时,我得到 RuntimeError: main thread is not in main loop 我不知道。在 GUI.py 中,我有线程,因此一个线程不断检查函数(在帮助程序中)以查看是否调用了它们中的任何一个。
  • 另外,一个全新的窗口打开了,我也不想要那个。 (一个空白的)......而beautifulChartWindow 永远不会打开。
猜你喜欢
  • 1970-01-01
  • 2020-03-30
  • 2013-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
相关资源
最近更新 更多