我正在制作一个简单的Tkinter计算器,但我似乎无法正确排列输入表格和按钮.
这是我目前的代码
from Tkinter import *
def calculate():
try:
num1 = float(enter1.get())
num2 = float(enter2.get())
result = num1 * num2
label3.config(text=str(result))
except ValueError:
label3.config(text='Enter numeric values!')
def calculate2():
try:
num1 = float(enter1.get())
num2 = float(enter2.get())
result = num1 / num2
label3.config(text=str(result))
except ValueError:
label3.config(text='Enter numeric values!')
def calculate3():
try:
num1 = float(enter1.get())
num2 = float(enter2.get())
result = num1 + num2
label3.config(text=str(result))
except ValueError:
label3.config(text='Enter numeric values!')
def calculate4():
try:
num1 = float(enter1.get())
num2 = float(enter2.get())
result = num1 - num2
label3.config(text=str(result))
except ValueError:
label3.config(text='Enter numeric values!')
root = Tk()
label1 = Label(root, text='First Number:')
label1.grid(row=0, column=1)
enter1 = Entry(root, bg='white')
enter1.grid(row=1, column=1)
label2 = Label(root, text='Second Number:')
label2.grid(row=2, column=1)
enter2 = Entry(root, bg='white')
enter2.grid(row=3, column=1)
btn1 = Button(root, text='X', command=calculate)
btn1.grid(row=4, column=1)
btn2 = Button(root, text='/', command=calculate2)
btn2.grid(row=5, column=1)
btn3 = Button(root, text='+', command=calculate3)
btn3.grid(row=5, column=2)
btn4 = Button(root, text='-', command=calculate4)
btn4.grid(row=4, column=2)
label3 = Label(root)
label3.grid(row=6, column=1)
enter1.focus()
enter1.bind('<Return>', func=lambda e:enter2.focus_set())
root.mainloop()
我想把它排成一行,然后制作它,所以加号和减号按钮都在输入表格下面,如果有人可以提供帮助那就太棒了谢谢你
解决方法:
您可以使用columnspan参数来跨越标签和条目两列.作为旁注,请记住列索引也是从零开始的:
label1.grid(row=0, column=0, columnspan=2)
enter1.grid(row=1, column=0, columnspan=2)
label2.grid(row=2, column=0, columnspan=2)
enter2.grid(row=3, column=0, columnspan=2)
btn1.grid(row=4, column=0)
btn2.grid(row=5, column=0)
btn3.grid(row=5, column=1)
btn4.grid(row=4, column=1)
label3.grid(row=6, column=0, columnspan=2)
另一个解决方案是创建一个新框架并将按钮放在其中,形成一个2乘2的简单网格.