我无法编辑 Tk.text 中的文本,因为它一直说函数未定义

问题描述 投票:0回答:1

我希望能够有两个功能,一个功能,get_text 将允许我检索当前在文本模块中的文本并将其打印出来。另一个函数 set_text,我希望能够用 set_text 函数中的输入替换当前在 tk.text 中的文本。

#!/usr/bin/python3
import tkinter as tk
import tkinter.ttk as ttk


class GetMethodsTestApp:
    def __init__(self, master=None):
        # build ui
        global text1

        toplevel1 = tk.Tk() if master is None else tk.Toplevel(master)
        toplevel1.configure(height=200, width=200)
        toplevel1.geometry("384x288")
        frame1 = ttk.Frame(toplevel1)
        frame1.configure(height=200, width=200)
        text1 = tk.Text(frame1)
        text1.configure(height=10, width=50)
        _text_ = 'result'
        text1.insert("0.0", _text_)
        text1.pack(side="top")
        frame1.grid(column=0, row=0, sticky="nsew")
        toplevel1.rowconfigure(0, weight=1)
        toplevel1.columnconfigure(0, weight=1)
        # Main widget
        self.mainwindow = toplevel1

    def run(self):
        self.mainwindow.mainloop()


    def get_text():
        msg = text1.get("0.0", len(_text_))
        print(msg)

    print(get_text())
    
    
    def set_text():
        add = input("Enter the text to replace in the string")
        while msg != add:
            add = input("Enter the text to replace in the string")

        
   print(set_text())     
        
if __name__ == "__main__":
    app = GetMethodsTestApp()
    app.run()

我想从 tk.text 中获取文本,但它不允许我访问它,即使我将其设为全局。 我想在 set_text 函数中设置文本,但它不允许我在 gui 中使用文本。

python user-interface tkinter ttk pygubu
1个回答
0
投票

_text_
不是全局定义的,它是
__init__
的本地定义。通过在其前面加上
self.
将其更改为实例变量,并将
self
参数添加到您的
GetMethodsTestApp
类方法

#!/usr/bin/python3
import tkinter as tk
# import tkinter.ttk as ttk
# it's more common to use the following:
from tkinter import ttk


class GetMethodsTestApp:
    def __init__(self, master=None):
        # build ui
        # global text1  # unnecessary
        toplevel1 = tk.Tk() if master is None else tk.Toplevel(master)
        toplevel1.configure(height=200, width=200)
        toplevel1.geometry("384x288")
        frame1 = ttk.Frame(toplevel1)
        frame1.configure(height=200, width=200)
        # make these 'instance variables' with 'self.'
        self.text1 = tk.Text(frame1)
        self.text1.configure(height=10, width=50)
        self._text_ = 'result'
        self.text1.insert("0.0", self._text_)  # prefix with 'self.'
        self.text1.pack(side="top")

        frame1.grid(column=0, row=0, sticky="nsew")
        toplevel1.rowconfigure(0, weight=1)
        toplevel1.columnconfigure(0, weight=1)
        # Main widget
        self.mainwindow = toplevel1

    def run(self):
        self.mainwindow.mainloop()

    def get_text(self):  # add 'self' parameter
        # you can access and modify instance objects via 'self.'
        msg = self.text1.get("0.0", len(self._text_))
        print(msg)
    
    def set_text(self):
        add = input("Enter the text to replace in the string")
        while msg != add:
            add = input("Enter the text to replace in the string")

      
if __name__ == "__main__":
    app = GetMethodsTestApp()
    app.run()

您可以通过

get
set
GetMethodsTestApp
内部调用
self.get_text()
self.set_text()
方法,或者您可以在类的实例上调用它们

© www.soinside.com 2019 - 2024. All rights reserved.