Home > Software design >  Python TKinter Mac: Buttons layout strangely & click don't work
Python TKinter Mac: Buttons layout strangely & click don't work

Time:06-20

Python TKinter Mac: Buttons layout strangely and when a button is clicked it looks the same (as if it hadn't been clicked) however I didn't check to see if the button worked .
Picture of issue and the space is a textbook

the Mac version is: a 2017 iMac running Monterey 12.4

    import tkinter as tk


window = tk.Tk()

window.geometry("500x500")
window.title("Stock Watchlist & Logger")

label = tk.Label(window, text="Tester", font=('Ariel', 18))
label.pack(padx=20, pady=20)


textbox = tk.Text(window, height=3, font=('Ariel', 16))
textbox.pack(padx=10, pady=10)

numbuttframe = tk.Frame(window)
numbuttframe.columnconfigure(0, weight=1)

yesnobuttframe = tk.Frame(window)
yesnobuttframe.columnconfigure(1, weight=1)

button1 = tk.Button(numbuttframe, text="1", font=('Arial', 18))
button1.grid(row=0, column=0, sticky=tk.W tk.E)

button2 = tk.Button(numbuttframe, text="2", font=('Arial', 18))
button2.grid(row=0, column=1, sticky=tk.W tk.E)

button3 = tk.Button(numbuttframe, text="3", font=('Arial', 18))
button3.grid(row=0, column=2, sticky=tk.W tk.E)

button4 = tk.Button(numbuttframe, text="4", font=('Arial', 18))
button4.grid(row=0, column=3, sticky=tk.W tk.E)

button5 = tk.Button(numbuttframe, text="5", font=('Arial', 18))
button5.grid(row=0, column=4, sticky=tk.W tk.E)

button6 = tk.Button(numbuttframe, text="6", font=('Arial', 18))
button6.grid(row=0, column=5, sticky=tk.W tk.E)

yesbutton = tk.Button(yesnobuttframe, text="Yes", font=('Arial', 18))
yesbutton.grid(row=0, column=0, sticky=tk.W tk.E)

nobutton = tk.Button(yesnobuttframe, text="No", font=('Arial', 18))
nobutton.grid(row=0, column=1, sticky=tk.W tk.E)

numbuttframe.pack(fill='x')
yesnobuttframe.pack(fill='x')

if button1:
    print("CELEBRATE!")

window.mainloop()

CodePudding user response:

You set the weight to two cells weight=1 , so they expanded, since the rest have a default weight of 0. Since I don’t see your grid - the buttons are placed in frames one after the other, suggest using the pack method. And shortened your code a bit. The buttons won't work yet, because they don't have a command = call. Complex variable names in Python are usually written with an underscore.

import tkinter as tk

FONT = ('Ariel', 18)

window = tk.Tk()

window.geometry("500x500")
window.title("Stock Watchlist & Logger")

lbl = tk.Label(window, text="Tester", font=FONT)
lbl.pack(padx=20, pady=20)


text_box = tk.Text(window, height=3, font=('Ariel', 16))
text_box.pack(padx=10, pady=10)

num_btn = tk.Frame(window)
yes_no_btn = tk.Frame(window)

num_btn.pack(fill='x')
yes_no_btn.pack(fill='x')

buttons = []

for i in range(1, 7):
    btn = tk.Button(num_btn, text=f"{i}", font=FONT)
    btn.pack(side="left", fill='x', expand=True)
    buttons.append(btn)


yes_btn = tk.Button(yes_no_btn, text="Yes", font=FONT)
yes_btn.pack(side="left", fill='x', expand=True)

no_btn = tk.Button(yes_no_btn, text="No", font=FONT)
no_btn.pack(side="left", fill='x', expand=True)


window.mainloop()
  • Related