1

Im trying to make it so on my (basic) tkinter program there is a button that changes the colors in order of a list, using a button, but when the button is pressed, nothing happens. Thoughts?

color = ['#65368c', '#279ffe', '#db4b4b', '#002a28']
coloriter = iter(color)
colorintk = str(next(coloriter))

root = tk.Tk()
root['background'] = colorintk
myFont = font.Font(family='comic sans')
random_button = tk.Button(root, text='Generate new name', command=random_name, bg=colorintk, font=myFont, )
random_button.pack()
color_button = tk.Button(root, text='New Color', command=next(coloriter), bg=colorintk, font=myFont, )
color_button.pack()

w = tk.Label(root, text=(random.choice(names) + " " + random.choice(names)), bg=colorintk, font=myFont)
w.pack()
root.geometry("640x640")
tk.mainloop()

note that there is more code which is what the random name and name variables are.

Blorpal
  • 19
  • 5
  • Your `colorintk` iterator is called once immediately at declaration, and won't ever change again. It's better to create a function that handles both name fetching *and* color setting to use as the button's `command`. Also, `coloriter = iter(color)` isn't necessary since lists (like `color`) are already iterables. – JRiggles Sep 28 '22 at 15:56
  • How would you go about doing that? – Blorpal Sep 28 '22 at 16:18
  • see below for my recommendation - it might look a little complex, but I promise it's not that bad. If you have any questions, let me know! The gist is that by using a `class`, you can more easily pass around things like variables (e.g., `color`) and widgets (e.g. `random_button`) between your `root` and other functions/methods. – JRiggles Sep 28 '22 at 16:33

1 Answers1

0

Here's my recommended solution following my comment above - some of your code has been omitted for brevity. I've wrapped your app in a simple class to allow info to be shared between the root and its methods (i.e., the randomize method defined below)

class Root(tk.Tk):
    def __init__(self):
        super().__init__()  # initialize tk.Tk
        self.geometry('640x640')
        self.title('Super Cool Randomizer 9000')  # or whatver...
        self.color = ['#65368c', '#279ffe', '#db4b4b', '#002a28']
        self.color_index = 0  # keep track of where we are in the color list
        
        # I promise I'm only judging you a little for using Comic Sans :P
        self.myFont = font.Font(family='comic sans')  

        # coloriter = iter(color) - unnecessary, lists are already iterable
        # colorintk = str(next(coloriter))

        # set the app window's background color
        self.configure(background=self.color[0])  # start w/ the first color in the list

        self.random_button = tk.Button(
            self,  # self always refers to the containing class
            text='Generate new name',
            command=self.randomize, 
            # we'll handle changing the bg in the 'randomize' method!
            bg=self.color[0],  # start w/ the first color in the list
            font=self.myFont,
        )
        self.random_button.pack()
    
    def randomize(self):
        self.color_index += 1  # go to the next color in the list
        # go back to the start of the color list once we reach the end
        if self.color_index >= len(self.color):
            self.color_index = 0
       
        new_color = self.color[self.color_index]  # get the next color
        # set the button and app background
        self.random_button.configure(bg=new_color)
        self.configure(background=new_color)
        # add whatever you need to for 'random names' here as well...


# run the app
if __name__ == '__main__':
    root = Root()
    root.mainloop()

FYI, you should generally use root.mainloop() instead of tk.mainloop(), or whatever you've named your tk.Tk() instance.

JRiggles
  • 4,847
  • 1
  • 12
  • 27