开发者

{PYTHON + TKINTER} "Type cast" problem: How to retrieve Custom frame objects from a Pmw.NoteBook?

开发者 https://www.devze.com 2023-03-24 07:41 出处:网络
I am writing a GUI Python application. I am using Tkinter + PythonMegaWidgets for semplicity reasons.

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, bec开发者_StackOverflow中文版ause 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


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
        ...
0

精彩评论

暂无评论...
验证码 换一张
取 消