Tkinter进入和网格

猎鹰用户

我正在尝试制作一个简单的计算器,并且希望在按钮上有一个输入小部件,但是网格无法正常工作。

这是我的代码:

from tkinter import *
w = 6
h = w - 3

root = Tk()
root.title("Calculator")

def callback():
    print("pressed")

e = Entry(root)
e.grid(row=1, column=1)

b1 = Button(root, bg='white', width=w, height=h, text="1", activebackground="black", activeforeground="white", command=callback)
b1.grid(row=2, column=1)

b2 = Button(root, bg='white', width=w, height=h, text="2", activebackground="black", activeforeground="white", command=callback)
b2.grid(row=2, column=2)

b3 = Button(root, bg='white', width=w, height=h, text="3", activebackground="black", activeforeground="white", command=callback)
b3.grid(row=2, column=3)

b4 = Button(root, bg='white', width=w, height=h, text="4", activebackground="black", activeforeground="white", command=callback)
b4.grid(row=3, column=1)

b5 = Button(root, bg='white', width=w, height=h, text="5", activebackground="black", activeforeground="white", command=callback)
b5.grid(row=3, column=2)

b6 = Button(root, bg='white', width=w, height=h, text="6", activebackground="black", activeforeground="white", command=callback)
b6.grid(row=3, column=3)

b7 = Button(root, bg='white', width=w, height=h, text="7", activebackground="black", activeforeground="white", command=callback)
b7.grid(row=4, column=1)

b8 = Button(root, bg='white', width=w, height=h, text="8", activebackground="black", activeforeground="white", command=callback)
b8.grid(row=4, column=2)

b9 = Button(root, bg='white', width=w, height=h, text="9", activebackground="black", activeforeground="white", command=callback)
b9.grid(row=4, column=3)

b0 = Button(root, bg='white', width=w, height=h, text="0", activebackground="black", activeforeground="white", command=callback)
b0.grid(row=5, column=2)

AC = Button(root, bg='white', width=w, height=h, text="AC", activebackground="black", activeforeground="white", command=callback)
AC.grid(row=5, column=1)

plus = Button(root, bg='white', width=w, height=h, text="+", activebackground="black", activeforeground="white", command=callback)
plus.grid(row=2, column=4)

minus = Button(root, bg='white', width=w, height=h, text="-", activebackground="black", activeforeground="white", command=callback)
minus.grid(row=3, column=4)

mult = Button(root, bg='white', width=w, height=h, text="×", activebackground="black", activeforeground="white", command=callback)
mult.grid(row=4, column=4)

div = Button(root, bg='white', width=w, height=h, text="÷", activebackground="black", activeforeground="white", command=callback)
div.grid(row=5, column=4)

equ = Button(root, bg='white', width=w, height=h, text="=", activebackground="black", activeforeground="white", command=callback)
equ.grid(row=5, column=3)

root.mainloop()

这是我的输出:输出

在仍然使用网格管理器的情况下,如何使程序的按钮与输入框对齐?

布莱恩·奥克利(Bryan Oakley)

一种简单的解决方案是让您的输入小部件跨越所有四列。

e.grid(row=1, column=1, columnspan=4)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python tkinter 和网格

来自分类Dev

TKinter 网格和继承对象

来自分类Dev

使用框架和网格的tkinter GUI布局

来自分类Dev

Tkinter网格内部的网格?

来自分类Dev

tkinter网格和框架不调整框架大小

来自分类Dev

使用 tkinter(和网格)在 IDLE 中添加图片

来自分类Dev

Tkinter网格怪异

来自分类Dev

Tkinter网格间距问题

来自分类Dev

使tkinter网格交错

来自分类Dev

python3 tkinter网格和打包,内联打包语法和优雅

来自分类Dev

Promise 进入 .then() 和 .catch()

来自分类Dev

Tkinter:网格还是在网格内部打包?

来自分类Dev

如何在Tkinter网格中向左对齐标签和输入框

来自分类Dev

使用Python Tkinter和网格布局管理器调整列表框的大小

来自分类Dev

网格中的Tkinter按钮对齐

来自分类Dev

tkinter网格对齐:Python 2.7

来自分类Dev

Tkinter对齐网格位置

来自分类Dev

TkInter网格功能错误消息

来自分类Dev

Tkinter Treeview网格对齐问题

来自分类Dev

Tkinter网格布局循环

来自分类Dev

Tkinter 网格系统:排列元素

来自分类Dev

网格空间中的间隙 - TKInter

来自分类Dev

类中的 Python Tkinter 网格

来自分类Dev

Tkinter 重叠网格小部件

来自分类Dev

滚动条进入python tkinter讨论

来自分类Dev

jQuery Draggable-防止网格对象进入相同位置

来自分类Dev

表面网格 - 去除边缘,循环进入无限循环 CGAL

来自分类Dev

在Tkinter中,padx用作小部件内的参数和网格函数中的参数时有何不同?

来自分类Dev

DLE和蓝牙网格