简体   繁体   中英

tkinter button pack in the same window

I want two buttons to show up when I press my rectangle (function door1), each one of them will give different true value to my global variable CHANGED. The problem is that I'm having trouble with the aligning. The buttons always show up in an additional part to my current window, and I want them to show up on top of my current window

from tkinter import Tk, Canvas, Button
CHANGED = False

def options():
    global window
    button1 = Button(window, text='Click to change door')
    button1.bind("<Button-1>", change)
    button1.pack(side='top')

    button2 = Button(window, text='Click to keep door')
    button2.bind("<Button-1>", keep)
    button2.pack(side='top')

def change(*args):
    global CHANGED
    CHANGED = True
def keep(*args):
    global CHANGED
    CHANGED = False 
def door1(*args):
    options()

window = Tk()
canvas = Canvas(window, width=600, height=500)
square1 = canvas.create_rectangle(50, 500, 150, 200, fill="blue", tags="open_door_1")

canvas.tag_bind("open_door_1", "<Button-1>", door1)

canvas.pack()
window.mainloop()

How can I change it?

在此处输入图片说明

You can use grid to achieve that, by defining the row to place your widgets :

from tkinter import Tk, Canvas, Button
CHANGED = False

def options():
    button1 = Button(window, text='Click to change door')
    button1.bind("<Button-1>", change)
    button1.grid(row=0, sticky='w')

    button2 = Button(window, text='Click to keep door')
    button2.bind("<Button-1>", keep)
    button2.grid(row=1, sticky='w')

def change(*args):
    global CHANGED
    CHANGED = True
def keep(*args):
    global CHANGED
    CHANGED = False
def door1(*args):
    options()

window = Tk()
canvas = Canvas(window, width=600, height=500)
square1 = canvas.create_rectangle(50, 500, 150, 200, fill="blue", tags="open_door_1")

canvas.tag_bind("open_door_1", "<Button-1>", door1)

canvas.grid(row=2)
window.mainloop()

EDIT:

A solution to keep the window from resizing when the widgets appear, you can use the rowconfigure method like below and specify a geometry :

Also, another point to fix : you should be careful about the creation of the buttons, each click on the canvas creates 2 more widgets (visible or not depending on the layout). The functional pattern may not be the best to handle this, you need to define the buttons as global variables and only create them if they are not already displayed :

from tkinter import Tk, Canvas, Button
CHANGED = False
button1 = None
button2 = None

def options():
    global button1, button2

    button1 = Button(window, text='Click to change door')
    button1.bind("<Button-1>", change)
    button1.grid(row=0, sticky='w')

    button2 = Button(window, text='Click to keep door')
    button2.bind("<Button-1>", keep)
    button2.grid(row=1, sticky='w')

def change(*args):
    global CHANGED
    CHANGED = True
def keep(*args):
    global CHANGED
    CHANGED = False
def door1(*args):
    if button1 is None and button2 is None:
        options()

window = Tk()
window.geometry("600x600")
window.rowconfigure(2,weight=1)
canvas = Canvas(window, width=600, height=500)
square1 = canvas.create_rectangle(50, 500, 150, 200, fill="blue", tags="open_door_1")

canvas.tag_bind("open_door_1", "<Button-1>", door1)    
canvas.grid(row=2,sticky='s')    
window.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM