首页 > 解决方案 > Tkinter 引用方法和类之间的变量

问题描述

我被困在. 中的类之间引用方法和变量Tkinter

这是一个简单的例子,我有三种不同类型的窗口,我想将它们放入不同的classes.

root窗口中,我可以单击button打开第二个窗口,我可以在其中向Text小部件输入内容。

同样在第二个窗口中,我希望OK按钮将Text小部件中的内容读取到第三个窗口中的insert另一个小部件中。TextCancel按钮可以关闭第二个窗口并root再次显示该窗口。

代码中有很多错误,因为我不知道如何在classes访问methods和之间进行交叉引用variables

任何人都可以帮助我实现这一目标吗?谢谢。

from tkinter import *
from tkinter import scrolledtext
    
    
    def main():
        """The main app function"""
        root = Tk()
        root_window = Root(root)
        return None
    
    
    class Root:
    
        def __init__(self, root):
            # Main root window configration
            self.root = root
            self.root.geometry("200x100")
            
            self.btn_ok = Button(self.root, text="Open new window",
                                 command=NewWindow)
            self.btn_ok.pack(padx=10, pady=10)
    
        def hide(self):
            """Hide the root window."""
            self.root.withdraw()
    
        def show(self):
            """Show the root window from the hide status"""
            self.root.update()
            self.root.deiconify()
    
        def onClosing(self, window):
            window.destroy()
            self.show()
    
    
    class NewWindow:
        
        def __init__(self):
    
            Root.hide()
        
            self.new_window = Toplevel()
    
            lbl = Label(self.new_window, text="Input here:")
            lbl.pack(padx=10, pady=(10, 0), anchor=W)
    
            # Create a scrolledtext widget.
            self.new_content = scrolledtext.ScrolledText(
                                    self.new_window, wrap=WORD,
                                    )
    
            self.new_content.pack(padx=10, expand=True, fill=BOTH, anchor=W)
    
    
            # Respond to the 'Cancel' button.
            btn_cancel = Button(self.new_window, text="Cancel", width=10,
                                command=lambda: Root.onClosing(self.new_window))
            btn_cancel.pack(padx=10, pady=10, side=RIGHT)
    
            # Add 'OK' button to read sequence
            self.btn_ok = Button(self.new_window, text="OK", width=10,
                                 command=WorkingWindow)
            self.btn_ok.pack(padx=10, pady=10, side=RIGHT)
    
        def readContent(self):
            self.content = self.new_content.get(1.0, END)
            self.new_window.destroy()
            workwindow = WorkingWindow()
    
    
    class WorkingWindow:
    
        def __init__(self):
    
            self.work_window = Toplevel()
            self.work_content = scrolledtext.ScrolledText(self.work_window, wrap=WORD, font=("Courier New", 11))
            self.work_content.pack(padx=10, expand=True, fill=BOTH, anchor=W)
            self.work_content.insert(1.0, Root.content)
    
    
    if __name__ == '__main__':
        main()
        

标签: pythonclasstkinter

解决方案


你几乎在那里你所要做的就是将 Root 类的实例传递给你正在调用的其他类

这是更正后的代码:

from tkinter import *
from tkinter import scrolledtext
    
    
def main():
        """The main app function"""
        root = Tk()
        root_window = Root(root)
        root.mainloop()
    
    
class Root:
    
        def __init__(self, root):
            # Main root window configration
            self.root = root
            self.root.geometry("200x100")
            
            self.btn_ok = Button(self.root, text="Open new window",
                                 command=lambda :NewWindow(self))
            self.btn_ok.pack(padx=10, pady=10)
    
        def hide(self):
            """Hide the root window."""
            self.root.withdraw()
    
        def show(self):
            """Show the root window from the hide status"""
            self.root.update()
            self.root.deiconify()
    
        def onClosing(self, window):
            window.destroy()
            self.show()
    
class NewWindow:
        
        def __init__(self, parent):
    
            parent.hide()
        
            self.new_window = Toplevel()
    
            lbl = Label(self.new_window, text="Input here:")
            lbl.pack(padx=10, pady=(10, 0), anchor=W)
    
            # Create a scrolledtext widget.
            self.new_content = scrolledtext.ScrolledText(
                                    self.new_window, wrap=WORD,
                                    )
    
            self.new_content.pack(padx=10, expand=True, fill=BOTH, anchor=W)
    
    
            # Respond to the 'Cancel' button.
            btn_cancel = Button(self.new_window, text="Cancel", width=10,
                                command=lambda: parent.onClosing(self.new_window))
            btn_cancel.pack(padx=10, pady=10, side=RIGHT)
    
            # Add 'OK' button to read sequence
            self.btn_ok = Button(self.new_window, text="OK", width=10,
                                 command=self.readContent)
            self.btn_ok.pack(padx=10, pady=10, side=RIGHT)
    
        def readContent(self):
            self.content = self.new_content.get(1.0, END)
            
            self.new_window.destroy()
            workwindow = WorkingWindow(self)
            
    
    
class WorkingWindow:
    
        def __init__(self, parent):
    
            self.work_window = Toplevel()
            self.work_content = scrolledtext.ScrolledText(self.work_window, wrap=WORD, font=("Courier New", 11))
            self.work_content.pack(padx=10, expand=True, fill=BOTH, anchor=W)
            self.work_content.insert(1.0, parent.content)
    
    
if __name__ == '__main__':
        main()


推荐阅读