[英]Change tkinter canvas's text outside the class
from tkinter import *
class MainBattle(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
global canvas
self.parent.title('Python')
self.pack(fill = BOTH, expand = 1)
canvas = Canvas(self)
self.Label_My = Label(self, text = 'MyObject')
self.Label_My.place(x = 470, y = 35)
canvas.pack(fill = BOTH, expand = 1)
canvas.update()
def aa(self):
self.Label_My['text'] = 'HisObject'
def Change():
Label_My['text'] = 'HisObject'
root = Tk()
ex = MainBattle(root)
root.geometry('700x500')
it should use global method? 应该使用全局方法吗? I would defind labels inside the class and change it's text outside class if possible.
我将在班级内部定义标签,并在可能的情况下在班级外部更改其文本。
You don't need global variables. 您不需要全局变量。 You have a reference to the instance, which allows you to access all instance variables:
您可以引用实例,从而可以访问所有实例变量:
ex.Label_My["text"] = "HisObject"
If your question is "can I use global to set variable values from outside the class" then yes. 如果您的问题是“我可以使用全局变量从类外部设置变量值”,那么可以。 Whenever you want to change the value of a global variable you need to write global.
每当您要更改全局变量的值时,都需要编写全局变量。
def changetext():
global label_text
label_text = "new text"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.