Montague27 Montague27 - 1 year ago 81
Python Question

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
def initUI(self):
global canvas
self.pack(fill = BOTH, expand = 1)
canvas = Canvas(self)
self.Label_My = Label(self, text = 'MyObject') = 470, y = 35)
canvas.pack(fill = BOTH, expand = 1)
def aa(self):
self.Label_My['text'] = 'HisObject'

def Change():
Label_My['text'] = 'HisObject'

root = Tk()
ex = MainBattle(root)

it should use global method?
I would defind labels inside the class and change it's text outside class if possible.

Answer Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download