{Python + TKINTER}“类型转换”问题:如何从 Pmw.NoteBook 中检索自定义框架对象?
我正在编写一个 GUI Python 应用程序。 出于简单原因,我使用 Tkinter + PythonMegaWidgets。
开门见山,我需要通过添加一些提供额外功能的自定义成员函数来扩展 Tkinter.Frame 基类。
这些“自定义框架”将添加到 Pmw.NoteBook 对象的单个选项卡中。
官方相关文档可以在以下位置找到: http://pmw.sourceforge.net/doc/NoteBook.html
之后,我需要从 NoteBook 中检索“自定义框架”实例,调用我添加的自定义成员函数;问题就开始了......
尽管遵循 Duck Typing 的原理,我无法访问这些“额外方法”中的任何一个,因为 Pmw.NoteBook 类的方法只能返回 Frame 对象!
我找不到任何解决方案。
下面是一段示例(更简化)代码,详细描述了我的问题。
from Tkinter import *
from Pmw import NoteBook
# I define a custom frame, by extending Tkinter.Frame baseclass
class CustomPanel(Frame):
def __init__(self, master, _text):
Frame.__init__(self, master)
self.label = Label(self, text=_text)
self.localVariable = "Hello, world!" # I define a new local variable
self.label.pack()
self.pack()
# I define a custom member function, which I _ABSOLUTELY_ want to be accessible.
def customMethod(self):
print self.localVariable
# main frame of application
class MyFrame(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.noteBook = NoteBook(self) # I create a NoteBook object...
tab1 = self.noteBook.add("tab 1") # then, I add one (empty) tabs to it
panel1 = CustomPanel(tab1, "hello")
self.button = Button(self, text="Call CustomMethod()!", command=self.callCustomMethod) # I add a button test
self.noteBook.grid()
self.button.grid(row=1)
self.pack()
self.mainloop()
# I define click handler for button,
def callCustomMethod(self):
panel1 = self.noteBook.page(0) # I try to get frame contained in current tab
# pane11 is supposed to be a 'CustomPanel' object;
try:
panel1.customMethod() # ...then, custom method should be accessible
except AttributeError:
print 'AttributeError!'
# for illustration purpose only, I show that Panel1 is a 'Frame'(superclass) object only!
print panel1.__class__
frame = MyFrame() # create a MyFrame instance
按下按钮,控制台输出为:
AttributeError!
Tkinter.Frame
预期反对:
1- 设置 panel1.class 属性,如下所示,
try:
panel1.__class__ = CustomPanel
panel1.customMethod() # ...then, custom method should be accessible
except AttributeError:
print 'AttributeError!'
不起作用,因为 customMethod() 在任何情况下都无法访问 localVariable,这仅在 CustomPanel 子类中声明;
2-我什至无法回忆起 CustomPanel 构造函数,因为这将重置原始成员变量,我想用它们的原始值检索它们。
任何帮助表示赞赏。
它
I am writing a GUI Python application.
I am using Tkinter + PythonMegaWidgets for semplicity reasons.
Going straight to the point, I need to extend Tkinter.Frame baseclass, by adding some custom member functions which provides extra-functionalities.
These "Custom Frames" will be added to single tabs of a Pmw.NoteBook object.
Official related docs can be found at: http://pmw.sourceforge.net/doc/NoteBook.html
Later, I need to retrieve "Custom Frame" instances from NoteBook, invoking custom member functions that I have added; here the problems begins...
despite the principle of Duck Typing, I can not access ANY of these "extra-methods" because the methods of Pmw.NoteBook class can only return Frame objects!.
I can not find any solution.
Below a piece of sample (more simplified) code which describes in detail my issue.
from Tkinter import *
from Pmw import NoteBook
# I define a custom frame, by extending Tkinter.Frame baseclass
class CustomPanel(Frame):
def __init__(self, master, _text):
Frame.__init__(self, master)
self.label = Label(self, text=_text)
self.localVariable = "Hello, world!" # I define a new local variable
self.label.pack()
self.pack()
# I define a custom member function, which I _ABSOLUTELY_ want to be accessible.
def customMethod(self):
print self.localVariable
# main frame of application
class MyFrame(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.noteBook = NoteBook(self) # I create a NoteBook object...
tab1 = self.noteBook.add("tab 1") # then, I add one (empty) tabs to it
panel1 = CustomPanel(tab1, "hello")
self.button = Button(self, text="Call CustomMethod()!", command=self.callCustomMethod) # I add a button test
self.noteBook.grid()
self.button.grid(row=1)
self.pack()
self.mainloop()
# I define click handler for button,
def callCustomMethod(self):
panel1 = self.noteBook.page(0) # I try to get frame contained in current tab
# pane11 is supposed to be a 'CustomPanel' object;
try:
panel1.customMethod() # ...then, custom method should be accessible
except AttributeError:
print 'AttributeError!'
# for illustration purpose only, I show that Panel1 is a 'Frame'(superclass) object only!
print panel1.__class__
frame = MyFrame() # create a MyFrame instance
Pressing the button, console output is:
AttributeError!
Tkinter.Frame
to anticipate objections:
1- Set panel1.class attribute, as showed below,
try:
panel1.__class__ = CustomPanel
panel1.customMethod() # ...then, custom method should be accessible
except AttributeError:
print 'AttributeError!'
DON'T work, because customMethod() could not access in any case to localVariable, which is declared in CustomPanel subclass only;
2- I can not even recall CustomPanel constructor, because this will RESET original member variables, which I want to retrieve with their original values.
Any help is appreciated.
IT
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不需要更改笔记本选项卡的类,您只需在该选项卡内添加自定义框架即可。只需告诉框架您的内框架是什么,并记住将您的内框架包装在标签框架内即可。
例如:
I don't think you need to change the class of the notebook tab, you can just add your custom frame inside that tab. Just tell the frame what your inner frame is, and remember to pack your inner frame inside the tab frame.
For example: