{Python + TKINTER}“类型转换”问题:如何从 Pmw.NoteBook 中检索自定义框架对象?

发布于 2024-11-27 08:23:58 字数 2726 浏览 1 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赤濁 2024-12-04 08:23:58

我认为您不需要更改笔记本选项卡的类,您只需在该选项卡内添加自定义框架即可。只需告诉框架您的内框架是什么,并记住将您的内框架包装在标签框架内即可。

例如:

class MyFrame(Frame):
    def __init__(self, master=None):
        ...
        panel1 = CustomPanel(tab1, "hello")
        panel1.pack(fill="both", expand=True)
        tab1.inner_panel = panel1
        ...

    def callCustomMethod(self):
        tab1 = self.noteBook.page(0) 
        panel1 = tab1.inner_panel
        ...

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:

class MyFrame(Frame):
    def __init__(self, master=None):
        ...
        panel1 = CustomPanel(tab1, "hello")
        panel1.pack(fill="both", expand=True)
        tab1.inner_panel = panel1
        ...

    def callCustomMethod(self):
        tab1 = self.noteBook.page(0) 
        panel1 = tab1.inner_panel
        ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文