Tkinter参考方法和类之间的变量 [英] Tkinter reference methods and variables between classes

查看:46
本文介绍了Tkinter参考方法和类之间的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am stuck at referencing methods and variables between classes in Tkinter.

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

Here is an simple example, I have three different types of windows, which I would like to put into different classes.

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

In the root window, I can click the button to open the second window, where I can input something in to the Text widget.

也是在第二个窗口中,我想要 OK 按钮读取 Text 小部件中的内容,然后将内容 insert 插入另一个文本窗口小部件进入第三个窗口.并且 Cancel 按钮可以关闭第二个窗口,并再次显示 root 窗口.

Also in the 2nd window I want the OK button to read the content in the Text widget and insert the content into another Text widget into the 3rd window. And the Cancel button can close the 2nd window and show the root window again.

代码中存在许多错误,因为我不知道如何在之间进行交叉引用以访问方法变量.

There is many bugs in the code, because I couldn't figure out how to make cross references between classes to access the methods and variables.

有人可以帮助我实现这一目标吗?谢谢.

Could anyone help me to accomplish that? Thanks.

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()
        

推荐答案

您几乎要做的就是将Root类的实例传递给您要调用的其他类

You were almost there all you have to do is pass the instance of Root class to other class you are calling

这是更正的代码:

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()

这篇关于Tkinter参考方法和类之间的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆