python - 从 python 3.x 中的外部文件应用框架?
问题描述
有没有办法将框架从外部模块应用到根窗口。我有 2 个文件:在
a.py
中有一行,我在一个导入b.pyself.root = root()
的类中有一个函数,
在
b.py
我有一个类并实例化一个 Frame 以显示在根窗口
中。没有错误,但框架小部件未显示在根窗口中。我尝试在b.py文件中更改 self.frame = Frame(root)
root
self.root
例如:
#file 'a'
class Root:
def __init__(self):
self.root = root()
root.title('Hello')
self.b = None
def boo(self):
import b
self.b = b.A()
Root.boo()
and
#file 'b'
class A:
def __init__(self):
self.frame = tk.LabelFrame(self.root)
self.frame.pack()
def __a_meth__(self):
Button(self.frame, text = 'YES')
Button.pack()
需要做哪些改变?
解决方案
通常你会传入任何需要的东西:
#file 'a'
class Root:
def __init__(self):
self.root = root()
self.root.title('Hello')
self.b = None
def boo(self):
import b
self.b = b.A(self.root) # pass the root object in
self.b.__a_meth__() # don't forget to call this if you want to see anything
Root.boo()
#file 'b'
class A:
def __init__(self, root):
self.root = root
self.frame = tk.LabelFrame(self.root)
self.frame.pack()
def __a_meth__(self):
Button(self.frame, text = 'YES')
Button.pack()
推荐阅读
- python - 将元组列表与数据框中的列进行比较
- vim - nvim 找到函数,但 vim 没有
- powerbi - 如何在 Power BI 中按州和市场查找表中的最低成本
- html - PowerShell Regex - 匹配不包含不同字符串的字符串
- rust - 尽管有限制,Rust `cannot infer type`
- javascript - 是否可以使用默认键名解构对象
- php - 从android传递参数到php的问题
- javascript - 为什么我的 boostrap 弹出框内容现在显示?
- c# - 需要一种获取当前请求 URL 以在多租户应用程序中配置数据库上下文的方法
- pandas - 根据下拉菜单中的选项值抓取表格