Python Tkinter,从课外修改Text [英] Python Tkinter, modify Text from outside the class

查看:62
本文介绍了Python Tkinter,从课外修改Text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从类的外部访问类 FirstPage 中定义的 Text 小部件.我试图通过创建 FirstPage 的新实例来解决这个问题,但找不到要使用的正确参数.还尝试使用 GUI 实例获取访问权限,但未成功.

I am trying to access the Text widget defined in class FirstPage from outside of the class. I tried to solve this problem by creating a new instance of FirstPage, but could not find the right arguments to use. Also tried to use instance of GUI to gain the access, but unsuccessfully.

当我可以在类之外使用 text.insert(0.0, t) 时,我的问题就解决了.它将帮助我通过与 GUI 不直接相关的功能修改用 Tkinter 显示的文本.

My problem is solved when I can use text.insert(0.0, t) from outside of the classes. It would help me modify the text displayed with Tkinter by functions that are not directly related with the GUI.

找到我尝试使用的代码的来源:在两帧之间切换在 tkinter 中

The origin of the code I am trying to use is found: Switch between two frames in tkinter

我还删除了这个问题不需要的行..

Also I removed lines that were not necessary for this question..

import Tkinter as tk

class GUI(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.geometry(self, '580x410')
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        frame = FirstPage(container, self)
        self.frames[FirstPage] = frame
        frame.grid(row=0, column=0, sticky="nsew")

        frame = self.frames[FirstPage]
        frame.tkraise()


class FirstPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        text = tk.Text(self , height=25, width=80)
        text.grid(column=0, row=0, sticky="nw")



app = GUI()
app.mainloop()

这是工作代码:

import Tkinter as tk

class GUI(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.geometry(self, '580x410')
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        frame = FirstPage(container, self)
        self.frames[FirstPage] = frame
        frame.grid(row=0, column=0, sticky="nsew")

        frame = self.frames[FirstPage]
        frame.tkraise()

        page_name = FirstPage.__name__
        self.frames[page_name] = frame


    def get_page(self, page_name):
        return self.frames[page_name]


class FirstPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.text = tk.Text(self , height=25, width=80)
        self.text.grid(column=0, row=0, sticky="nw")



app = GUI()
app.get_page("FirstPage").text.insert("1.0", "Hello, world")
app.mainloop()

推荐答案

您无需做任何特别的事情.与任何 Python 对象一样,您只需要引用该对象即可对其进行操作.

There's nothing special you need to do. As with any python object, you simply need a reference to the object in order to manipulate it.

您开始使用的代码中的概念是有一个控制器"来控制对所有页面的访问,因为该对象是创建页面的地方.您可以在控制器中添加一个函数,为您提供对页面的引用,然后您可以使用它来调用该页面上的函数.

The concept in the code you started with is to have a "controller" that controls access to all of the pages, since that object is where the pages are created. You can add a function in the controller that gives you a reference to a page, and then you can use that to call a function on that page.

以下是您需要对控制器进行的更改:

Here's the changes you need to make to the controller:

class GUI(tk.Tk):

    def __init__(self, *args, **kwargs):
        ...
        page_name = FirstPage.__name__
        self.frames[page_name] = frame
        ...

    def get_page(self, page_name):
        return self.frames[page_name]

您还需要修改 FirstPage 以保留对小部件的引用,以便您以后可以访问它:

You also need to modify FirstPage to keep a reference to the widget so that you can access it later:

class FirstPage(tk.Frame):
    def __init__(self, parent, controller):
        ...
        self.text = tk.Text(...)
        ...

您现在可以在任何其他代码中通过 get_page 访问文本小部件(但您的页面必须保存对控制器的引用才能使其工作).

From within any other code you can now access the text widget via get_page (but your pages must save a reference to the controller for this to work).

class AnotherPage(tk.Frame):

    def __init__(self, parent, controller):
        ...
        self.controller = controller
        ...

    def some_function(self):
        ...
        first_page = self.controller.get_page("FirstPage")
        text = first_page.text.get("1.0", "end-1c")
        ...
        first_page.text.insert("end", "some new text\n")

请注意,此技术适用于任何 GUI 页面之外.在您的代码中,app 是控制器,因此您可以执行以下操作:

Note that this technique works outside of any GUI pages. In your code, app is the controller, so you can do something like this:

app = GUI()
app.get_page("FirstPage").text.insert("1.0", "Hello, world")

这篇关于Python Tkinter,从课外修改Text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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