Python Forum

Full Version: Configure label from different class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi I stack, i can't change image in config via main class:

class SETUP:

    def __init__(self, root):
        self.root = root
        self.frame = Frame(self.root)
#button for open new window help
        self.button = Button(self.root, command=self.help)
        self.button.place(x=0, y=0)
#button for change image for help
        self.button = Button(self.root, command=self.change_backgound_class_hhelp)
        self.button.place(x=100, y=0)

    def change_backgound_class_hhelp(self):     #<<<<<<<here is problem!
            self.hhelp.config(root, image=help2_bg)

# def for new window create
    def help(self):
        self.newWindow2 = Toplevel(self.root)
        bb = hhelp(self.newWindow2)


class hhelp():
    def __init__(self, root):
        self.root = root
        self.frame2 = Frame(self.root)
        self.frame2.pack()
        self.bgg = Label(self.root,image=help1_bg).place(x=0, y=0, relwidth=1, relheight=1)

if __name__ == '__main__':
    root = Tk()
    help1_bg = ImageTk.PhotoImage(Image.open("help.jpg"))
    help2_bg = ImageTk.PhotoImage(Image.open("help1.jpg"))
    root.overrideredirect(0)
    b = SETUP(root)
    root.mainloop()
Okay several things wrong from what I can see
  1. within the Class Setup you instantiate the Class hhelp within the help() method as "bb"
  2. when you instantiate Class hhelp as "bb" you never anchor it to the Setup Class using "self."
  3. You call both buttons "self.button" which really means you only have one button that is performing that
    second function (as it was the last assignment made) but we have already established that the first button is
    what instantiates that class that the second button calls
I would strongly suggest instantiating hhelp as part of the Setup Class __init__ otherwise you might
encounter errors because someone presses the 2nd button first -- I would definitely state that you need to
name your 2 buttons something different like self.btnOpen1 and self.btnOpen2 but it would be even better to
name them more appropriately based on what they do. If you trying to have the button do two different things
then you would have the button call a function that then called whatever functionality you wanted it to perform
self.btnOpen = Button(self.root, command=self.DoOneOfThese)

def DoOneOfThese()
    if self.YourCriterion:
        self.help()
    else:
        self.change_background_class_hhelp()