Home > OS >  How to access label from one function to another function
How to access label from one function to another function

Time:11-20

I am new to tkinter. I have defined a label in one function on a button click. When I try to access that label in different function on another button click, it is throwing error. How to access label between two functions? How to make label global?

my code:

from tkinter import *
from tkinter import messagebox

root = Tk()


def Update():
    if(my_warning.cget("text") == "Warning: Click on “Validate Content“ button" or my_warning.cget("text") == "Content validation is failed!"):
        messagebox.showwarning("Warning", "Validate the XML file")
    else:
        messagebox.showinfo("Status", "All good")
        

global stuff

stuff = "XML is good"


def validate_file():
    global stuff
    
    if(stuff == "XML is good"):
        my_warning = Label(root, text= "Content validation is successful!", fg = "green", font = ("Helvetica", 11))
        my_warning.grid(row = 8, column = 0, pady = (20,0))
    elif(stuff1 == "XML is not good"):
        my_warning = Label(root, text= "Content validation is failed!", fg = "red", font = ("Helvetica", 11))
        my_warning.grid(row = 8, column = 0, pady = (20,0))
    else:
        my_warning = Label(root, text= "Warning: Click on “Validate Content“ button", fg = "red", font = ("Helvetica", 11))
        my_warning.grid(row = 8, column = 0, pady = (20,0))


validate_button = Button(
    root,
    text='Validate',
    command=validate_file, height = 1, width = 14, font = ("Helvetica", 11)
)
validate_button.grid(row = 3, column = 0, padx = (0,50), pady =20)

update_button = Button(
    root,
    text='Update',
    command=Update, height = 1, width = 12, font = ("Helvetica", 11)
)
update_button.grid(row = 3, column = 1, padx = (300,200), pady =20)

root.mainloop()

Error I got: enter image description here

enter image description here

CodePudding user response:

This is a bit better structured code, where all the related variables are collected in the TKValidator class here. There are pieces missing, such as where text is coming from. I also took the liberty to change some of the variable names to show their purposes better.

from tkinter import *
from tkinter import messagebox


class TKValidator:
    def __init__(self):
        self.root = Tk()
        self.validate_button = Button(
            self.root,
            text='Validate',
            command=self.validate_file, height=1, width=14, font=("Helvetica", 11)
        )
        self.validate_button.grid(row=3, column=0, padx=(0, 50), pady=20)
        self.update_button = Button(
            self.root,
            text='Update',
            command=self.update, height=1, width=12, font=("Helvetica", 11)
        )
        self.update_button.grid(row=3, column=1, padx=(300, 200), pady=20)

        self.text = None
        self.message = None

    def update(self):
        if self.message is None:
            messagebox.showwarning("Error", "Oopsie, validate needs to be called first")
        elif self.message.cget("text") == "Warning: Click on “Validate Content“ button" or self.message.cget("text") == "Content validation is failed!":
            messagebox.showwarning("Warning", "Validate the XML file")
        else:
            messagebox.showinfo("Status", "All good")
        
    def validate_file(self):
        if self.text == "XML is good":
            self.message = Label(self.root, text= "Content validation is successful!", fg="green", font=("Helvetica", 11))
            self.message.grid(row=8, column=0, pady=(20, 0))
        elif self.text == "XML is not good":
            self.message = Label(self.root, text= "Content validation is failed!", fg="red", font=("Helvetica", 11))
            self.message.grid(row=8, column=0, pady=(20, 0))
        else:
            self.message = Label(self.root, text= "Warning: Click on “Validate Content“ button", fg="red", font=("Helvetica", 11))
            self.message.grid(row=8, column=0, pady=(20, 0))

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

validator = TKValidator()
validator.text = "XML is good"
validator.run()

CodePudding user response:

Define a my_warning variable outside the scope of your function that is empty like:

my_warning = ''

This should resolve your issue. But still not sure how you will .cget a value.

  • Related