Tkinter:从另一个模块调用应用程序功能
Tkinter: Calling an app function from another module
我创建了一个小虚拟项目来举例说明我的 objective 是什么:
该应用程序由两个小部件组成,一个按钮和一个文本区域。按下按钮时,调用另一个模块。被调用模块然后回调到主机并访问“插入文本”功能,就像被调用模块中代码运行的日志一样。
下面是我试图实现的一个简单示例。
项目结构为:
main.py
另一个(文件夹)
- module.py
main.py
from tkinter import *
import tkinter as tk
#from tkmacosx import Button ignore if not in MacOS
from another import module
class Example():
def __init__(self,parent):
self.main_frame = Frame(root)
self.main_frame.pack()
self.text = Text(self.main_frame)
self.text.grid(row = 0, column = 0)
self.button = Button(self.main_frame,
height = 25,
width = 150,
command = self.call_another_module)
self.button.grid(row = 1, column = 0)
self.new_text_line("Start.")
def call_another_module(self):
self.new_text_line("button press!\n")
module.Generic_class()
def new_text_line(self, message):
self.text.insert(INSERT, message + '\n')
if __name__ == '__main__':
root = Tk()
root.title("Example")
app = Example(root)
root.mainloop()
module.py
import tkinter as tk
#Shenanigans to import main
import os,sys,inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir)
import main
class Generic_class():
#code of something
#call new_text_line function
main.Example().new_text_line("Hurray!")
#continue code
我知道调用上面示例中显示的主模块是不正确的,因为我正在再次调用应用程序实例。
非常感谢有关此特定问题的反馈以及解决此问题的其他方法。
查看其他小部件 - 所有这些小部件都将其他小部件作为第一个参数,它是它的父级。
在你的 class 中做同样的事情 - 获取父窗口小部件作为第一个参数
class GenericClass(): # PEP8: `UpperCaseNames` for classes
def __init__(self, parent):
self.parent = parent
self.parent.new_text_line("Hurray!")
然后就可以创建了
module.GenericClass(self)
并且它将可以访问 Example
中的所有 functions/variables
我创建了一个小虚拟项目来举例说明我的 objective 是什么:
该应用程序由两个小部件组成,一个按钮和一个文本区域。按下按钮时,调用另一个模块。被调用模块然后回调到主机并访问“插入文本”功能,就像被调用模块中代码运行的日志一样。 下面是我试图实现的一个简单示例。
项目结构为:
main.py
另一个(文件夹)
- module.py
main.py
from tkinter import *
import tkinter as tk
#from tkmacosx import Button ignore if not in MacOS
from another import module
class Example():
def __init__(self,parent):
self.main_frame = Frame(root)
self.main_frame.pack()
self.text = Text(self.main_frame)
self.text.grid(row = 0, column = 0)
self.button = Button(self.main_frame,
height = 25,
width = 150,
command = self.call_another_module)
self.button.grid(row = 1, column = 0)
self.new_text_line("Start.")
def call_another_module(self):
self.new_text_line("button press!\n")
module.Generic_class()
def new_text_line(self, message):
self.text.insert(INSERT, message + '\n')
if __name__ == '__main__':
root = Tk()
root.title("Example")
app = Example(root)
root.mainloop()
module.py
import tkinter as tk
#Shenanigans to import main
import os,sys,inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir)
import main
class Generic_class():
#code of something
#call new_text_line function
main.Example().new_text_line("Hurray!")
#continue code
我知道调用上面示例中显示的主模块是不正确的,因为我正在再次调用应用程序实例。
非常感谢有关此特定问题的反馈以及解决此问题的其他方法。
查看其他小部件 - 所有这些小部件都将其他小部件作为第一个参数,它是它的父级。
在你的 class 中做同样的事情 - 获取父窗口小部件作为第一个参数
class GenericClass(): # PEP8: `UpperCaseNames` for classes
def __init__(self, parent):
self.parent = parent
self.parent.new_text_line("Hurray!")
然后就可以创建了
module.GenericClass(self)
并且它将可以访问 Example