[Python] How do I add a button into python that will lead to another button in Python?

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm brand new to Python. I'm working on this small project that is going well. It's a food locator for my area. Basically, you select the city, then the price range, and finally the cuisine. The program will take those inputs and redirect you to a Yelp page. I worked on how to get multiple buttons at once. I'm having trouble coding one button stemming from another - clicking city, then price, then cuisine. I've searched the web and nothing seems to work. I did learn about GUIs, and I'd like to avoid them right now.

    Here's my code:

    import tkinter as tk
    HEIGHT = 300
    WIDTH = 500

    root = tk.Tk()

    root.title("Food Locator")

    canvas = tk.Canvas(root, height = HEIGHT, width = WIDTH)
    canvas.pack()


    frame = tk.Frame(root, bg = '#8BDFF4')
    frame.place(relx = 0.1, rely = 0.1, relwidth = 0.8, relheight = 0.5)
    #Function to destroy the frame
    def change():
    frame.destroy()
    HEIGHT = 300
    WIDTH = 500

    canvas = tk.Canvas(root, height = HEIGHT, width = WIDTH)
    canvas.pack()
    #Renamed the second frame
    framee = tk.Frame(root, bg = '#8BDFF4')
    framee.place(relx = 0.1, rely = 0.1, relwidth = 0.8, relheight = 0.5)

    button1 = tk.Button(frame, text = "inflows", bg = '#8BF4AB', fg = 'black', activebackground='#62AF7A')
    button1.place(relx = 0, rely = 0.35, relwidth = 0.25,relheight = 0.25)

    #command=change, tells the button what function should run when it's clicked
    button1 = tk.Button(frame, text = "button1", bg = '#8BF4AB', fg = 'black', activebackground='#62AF7A',command=change)
    button1.place(relx = 0, rely = 0, relwidth = 0.25,relheight = 0.25)


    #new button

    button2 = tk.Button(frame, text = "inflows", bg = '#8BF4AB', fg = 'black', activebackground='#62AF7A')
    button2.place(relx = 0.25, rely = 0.35, relwidth = 0.25,relheight = 0.25)


    #command=change, tells the button what function should run when it's clicked
    button2 = tk.Button(frame, text = "button2", bg = '#8BF4AB', fg = 'black', activebackground='#62AF7A',command=change)
    button2.place(relx = 0.25, rely = 0, relwidth = 0.25,relheight = 0.25)

    #new button

    button3 = tk.Button(frame, text = "inflows", bg = '#8BF4AB', fg = 'black', activebackground='#62AF7A')
    button2.place(relx = 0.25, rely = 0.35, relwidth = 0.25,relheight = 0.25)


    #command=change, tells the button what function should run when it's clicked
    button3 = tk.Button(frame, text = "button3", bg = '#8BF4AB', fg = 'black', activebackground='#62AF7A',command=change)
    button3.place(relx = 0.50, rely = 0, relwidth = 0.25,relheight = 0.25)

    #new button

    button4 = tk.Button(frame, text = "inflows", bg = '#8BF4AB', fg = 'black', activebackground='#62AF7A')
    button2.place(relx = 0.25, rely = 0.35, relwidth = 0.25,relheight = 0.25)


    #command=change, tells the button what function should run when it's clicked
    button4 = tk.Button(frame, text = "button4", bg = '#8BF4AB', fg = 'black', activebackground='#62AF7A',command=change)
    button4.place(relx = 0.75, rely = 0, relwidth = 0.25,relheight = 0.25)

    root.mainloop()


    If anyone can give me any advice on how to do my button project, it'll be greatly appreciated.

    Continue reading...

Compartilhe esta Página