Python Forum

Full Version: Tkinter Buttons action
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
First of all, I do not have any code yet to show.

I am new to python and coding in generel, so please if you can, explain in simple english. :).

*I am using Tkinter*
So i want a window with a set of buttons. eg 2 buttons you can press.
So far so good. I can do that.

But when i press a button i want the window to show a new set of buttons.

eg there are two buttons in the "first" window.
if you press button 1 - 2 new buttons will appear and the first 2 will disappear
if you press button 2 - 4 new buttons will appear and the first 2 will disappear

I have searched the internet hours for this, but cant seem to find anything explaining this.

I want it all in the same window.
when you define your button, include a 'command' attribute which will contain the address of function you wish to execute when button is pressed (when I say address, I am refering to the function name minus the () which would cause it to execute.
Example:
import tkinter as tk

def clicked():
    print("you pressed the button")
    # add logic here:
    # has new set of buttins been declared?
    # if not, define new set

def main():
    root = tk.Tk()
    # give it sone size
    root.geometry('100x100+10+10')
    frame = tk.Frame(root)
    frame.pack(fill=tk.BOTH)

    button = tk.Button(frame, text='Click Me', command=clicked)
    button.pack()

    root.mainloop()

if __name__ == '__main__':
    main()