Как выровнять кнопки по левому краю в tkinter
Вот код:
import tkinter as tk
import math
def fact():
try:
result.set(math.factorial(int(num.get())))
except ValueError:
result.set('Invalid input')
def sqrt():
try:
result.set(math.sqrt(float(num.get())))
except ValueError:
result.set('Invalid input')
def logo():
try:
result.set(math.log(float(num.get())))
except ValueError:
result.set('Invalid input')
def acos():
try:
result.set(math.acos(float(num.get())))
except ValueError:
result.set('Invalid input')
def asin():
try:
result.set(math.asin(float(num.get())))
except ValueError:
result.set('Invalid input')
def atan():
try:
result.set(math.atan(float(num.get())))
except ValueError:
result.set('Invalid input')
def acosh():
try:
result.set(math.acosh(float(num.get())))
except ValueError:
result.set('Invalid input')
def asinh():
try:
result.set(math.asinh(float(num.get())))
except ValueError:
result.set('Invalid input')
def atanh():
try:
result.set(math.atanh(float(num.get())))
except ValueError:
result.set('Invalid input')
def cosh():
try:
result.set(math.cosh(float(num.get())))
except ValueError:
result.set('Invalid input')
def sinh():
try:
result.set(math.sinh(float(num.get())))
except ValueError:
result.set('Invalid input')
def tanh():
try:
result.set(math.tanh(float(num.get())))
except ValueError:
result.set('Invalid input')
def cos():
try:
result.set(math.cos(float(num.get())))
except ValueError:
result.set('Invalid input')
def sin():
try:
result.set(math.sin(float(num.get())))
except ValueError:
result.set('Invalid input')
def tan():
try:
result.set(math.cosh(float(num.get())))
except ValueError:
result.set('Invalid input')
root = tk.Tk()
root.title("ComPortable Calculator version 1.2.0")
root.geometry('260x400')
# canvas = tk.Canvas
# canvas.create_line(900, 30, 5, 30)
num = tk.StringVar()
result = tk.StringVar()
numb = tk.Label(root, text="Number")
numb.grid(column=2, row=1)
numbenter = tk.Entry(root, textvariable=num)
numbenter.grid(column=3, row=1)
b1 = tk.Button(root, text="Factorial", command=fact)
b1.grid(column=0, row=1)
b2 = tk.Button(root, text="Sqrt", command=sqrt)
b2.grid(column=0, row=2)
b3 = tk.Button(root, text="Logarithm", command=logo)
b3.grid(column=0, row=3)
b4 = tk.Button(root, text="ArcCos", command=acos)
b4.grid(column=0, row=4)
b5 = tk.Button(root, text="ArcSin", command=asin)
b5.grid(column=0, row=5)
b6 = tk.Button(root, text="ArcTan", command=atan)
b6.grid(column=0, row=6)
b7 = tk.Button(root, text="Cos", command=cos)
b7.grid(column=0, row=7)
b8 = tk.Button(root, text="Sin", command=sin)
b8.grid(column=0, row=8)
b9 = tk.Button(root, text="Tan", command=tan)
b9.grid(column=0, row=9)
b10 = tk.Button(root, text="ArcCosH", command=acosh)
b10.grid(column=0, row=10)
b11 = tk.Button(root, text="ArcSinH", command=asinh)
b11.grid(column=0, row=11)
b12 = tk.Button(root, text="ArcTanH", command=atanh)
b12.grid(column=0, row=12)
b13 = tk.Button(root, text="CosH", command=cosh)
b13.grid(column=0, row=13)
b14 = tk.Button(root, text="SinH", command=sinh)
b14.grid(column=0, row=14)
b15 = tk.Button(root, text="TanH", command=tanh)
b15.grid(column=0, row=15)
result1 = tk.Label(root, text="Результат")
result1.grid(column=2, row=3)
result2 = tk.Entry(root, textvariable=result, state='readonly')
result2.grid(column=3, row=3)
root.mainloop()
Ответы (3 шт):
Чтобы настроить размер кнопки, мы можем использовать свойства ширины и высоты виджета кнопки.
import tkinter as tk
import math
def fact():
try:
result.set(math.factorial(int(num.get())))
except ValueError:
result.set('Invalid input')
def sqrt():
try:
result.set(math.sqrt(float(num.get())))
except ValueError:
result.set('Invalid input')
def logo():
try:
result.set(math.log(float(num.get())))
except ValueError:
result.set('Invalid input')
def acos():
try:
result.set(math.acos(float(num.get())))
except ValueError:
result.set('Invalid input')
def asin():
try:
result.set(math.asin(float(num.get())))
except ValueError:
result.set('Invalid input')
def atan():
try:
result.set(math.atan(float(num.get())))
except ValueError:
result.set('Invalid input')
def acosh():
try:
result.set(math.acosh(float(num.get())))
except ValueError:
result.set('Invalid input')
def asinh():
try:
result.set(math.asinh(float(num.get())))
except ValueError:
result.set('Invalid input')
def atanh():
try:
result.set(math.atanh(float(num.get())))
except ValueError:
result.set('Invalid input')
def cosh():
try:
result.set(math.cosh(float(num.get())))
except ValueError:
result.set('Invalid input')
def sinh():
try:
result.set(math.sinh(float(num.get())))
except ValueError:
result.set('Invalid input')
def tanh():
try:
result.set(math.tanh(float(num.get())))
except ValueError:
result.set('Invalid input')
def cos():
try:
result.set(math.cos(float(num.get())))
except ValueError:
result.set('Invalid input')
def sin():
try:
result.set(math.sin(float(num.get())))
except ValueError:
result.set('Invalid input')
def tan():
try:
result.set(math.cosh(float(num.get())))
except ValueError:
result.set('Invalid input')
root = tk.Tk()
root.title("ComPortable Calculator version 1.2.0")
root.geometry('260x400')
# canvas = tk.Canvas
# canvas.create_line(900, 30, 5, 30)
num = tk.StringVar()
result = tk.StringVar()
numb = tk.Label(root, text="Number")
numb.grid(column=2, row=1)
numbenter = tk.Entry(root, textvariable=num)
numbenter.grid(column=3, row=1)
# -----------------------------------> vvvvvvvv <----------------- !!! +++
b1 = tk.Button(root, text="Factorial", width=10, command=fact)
b1.grid(column=0, row=1)
b2 = tk.Button(root, text="Sqrt", width=10, command=sqrt)
b2.grid(column=0, row=2)
b3 = tk.Button(root, text="Logarithm", width=10, command=logo)
b3.grid(column=0, row=3)
b4 = tk.Button(root, text="ArcCos", width=10, command=acos)
b4.grid(column=0, row=4)
b5 = tk.Button(root, text="ArcSin", width=10, command=asin)
b5.grid(column=0, row=5)
b6 = tk.Button(root, text="ArcTan", width=10, command=atan)
b6.grid(column=0, row=6)
b7 = tk.Button(root, text="Cos", width=10, command=cos)
b7.grid(column=0, row=7)
b8 = tk.Button(root, text="Sin", width=10, command=sin)
b8.grid(column=0, row=8)
b9 = tk.Button(root, text="Tan", width=10, command=tan)
b9.grid(column=0, row=9)
b10 = tk.Button(root, text="ArcCosH", width=10, command=acosh)
b10.grid(column=0, row=10)
b11 = tk.Button(root, text="ArcSinH", width=10, command=asinh)
b11.grid(column=0, row=11)
b12 = tk.Button(root, text="ArcTanH", width=10, command=atanh)
b12.grid(column=0, row=12)
b13 = tk.Button(root, text="CosH", width=10, command=cosh)
b13.grid(column=0, row=13)
b14 = tk.Button(root, text="SinH", width=10, command=sinh)
b14.grid(column=0, row=14)
b15 = tk.Button(root, text="TanH", width=10, command=tanh)
b15.grid(column=0, row=15)
result1 = tk.Label(root, text="Результат")
result1.grid(column=2, row=3)
result2 = tk.Entry(root, textvariable=result, state='readonly')
result2.grid(column=3, row=3)
root.mainloop()
У метода .grid()
есть параметр sticky
для прилипания виджета к указанным сторонам ячейки грида. Например, если указать tk.W
, то прилипнет к "западной" (west) стороне, т.е. к левой:
b1 = tk.Button(root, text="Factorial", command=fact)
b1.grid(column=0, row=1, sticky=tk.W)
b2 = tk.Button(root, text="Sqrt", command=sqrt)
b2.grid(column=0, row=2, sticky=tk.W)
b3 = tk.Button(root, text="Logarithm", command=logo)
b3.grid(column=0, row=3, sticky=tk.W)
b4 = tk.Button(root, text="ArcCos", command=acos)
b4.grid(column=0, row=4, sticky=tk.W)
b5 = tk.Button(root, text="ArcSin", command=asin)
b5.grid(column=0, row=5, sticky=tk.W)
b6 = tk.Button(root, text="ArcTan", command=atan)
b6.grid(column=0, row=6, sticky=tk.W)
b7 = tk.Button(root, text="Cos", command=cos)
b7.grid(column=0, row=7, sticky=tk.W)
b8 = tk.Button(root, text="Sin", command=sin)
b8.grid(column=0, row=8, sticky=tk.W)
b9 = tk.Button(root, text="Tan", command=tan)
b9.grid(column=0, row=9, sticky=tk.W)
b10 = tk.Button(root, text="ArcCosH", command=acosh)
b10.grid(column=0, row=10, sticky=tk.W)
b11 = tk.Button(root, text="ArcSinH", command=asinh)
b11.grid(column=0, row=11, sticky=tk.W)
b12 = tk.Button(root, text="ArcTanH", command=atanh)
b12.grid(column=0, row=12, sticky=tk.W)
b13 = tk.Button(root, text="CosH", command=cosh)
b13.grid(column=0, row=13, sticky=tk.W)
b14 = tk.Button(root, text="SinH", command=sinh)
b14.grid(column=0, row=14, sticky=tk.W)
b15 = tk.Button(root, text="TanH", command=tanh)
b15.grid(column=0, row=15, sticky=tk.W)
Если указать tk.EW
(East-West), то все виджеты расширятся под ширину своих ячеек, и станут одного размера по ширине:
b1 = tk.Button(root, text="Factorial", command=fact)
b1.grid(column=0, row=1, sticky=tk.EW)
b2 = tk.Button(root, text="Sqrt", command=sqrt)
b2.grid(column=0, row=2, sticky=tk.EW)
b3 = tk.Button(root, text="Logarithm", command=logo)
b3.grid(column=0, row=3, sticky=tk.EW)
b4 = tk.Button(root, text="ArcCos", command=acos)
b4.grid(column=0, row=4, sticky=tk.EW)
b5 = tk.Button(root, text="ArcSin", command=asin)
b5.grid(column=0, row=5, sticky=tk.EW)
b6 = tk.Button(root, text="ArcTan", command=atan)
b6.grid(column=0, row=6, sticky=tk.EW)
b7 = tk.Button(root, text="Cos", command=cos)
b7.grid(column=0, row=7, sticky=tk.EW)
b8 = tk.Button(root, text="Sin", command=sin)
b8.grid(column=0, row=8, sticky=tk.EW)
b9 = tk.Button(root, text="Tan", command=tan)
b9.grid(column=0, row=9, sticky=tk.EW)
b10 = tk.Button(root, text="ArcCosH", command=acosh)
b10.grid(column=0, row=10, sticky=tk.EW)
b11 = tk.Button(root, text="ArcSinH", command=asinh)
b11.grid(column=0, row=11, sticky=tk.EW)
b12 = tk.Button(root, text="ArcTanH", command=atanh)
b12.grid(column=0, row=12, sticky=tk.EW)
b13 = tk.Button(root, text="CosH", command=cosh)
b13.grid(column=0, row=13, sticky=tk.EW)
b14 = tk.Button(root, text="SinH", command=sinh)
b14.grid(column=0, row=14, sticky=tk.EW)
b15 = tk.Button(root, text="TanH", command=tanh)
b15.grid(column=0, row=15, sticky=tk.EW)
Параметры можно брать из tkinter - типа tk.NSEW
, но можно складывать одиночные tk.W + tk.E
, либо использовать просто строку вида "nsew"
.
Я хотел бы добавить к комментариям уважаемых консультантов, что нет нужды стучать лишний раз по клавиатуре, а именно, вместо sticky=tk.W можно писать sticky="w". В других аналогичных командах типа tk.END - то же самое. А в чём собственно проблема? Да никакой вобщем-то. Кроме той, что можно не привязываться намертво к имечку "tk".