当前位置: 首页 > 面试题库 >

Tkinter按钮使用网格扩展

杨选
2023-03-14
问题内容

我是编码的新手,只是学习Python。我搜索了答案,但找不到答案。

我正在尝试制作一个用于学习目的的计算器,但无法使底部keyboard frame扩展或buttons内部扩展以匹配根窗口的边界

这是我的代码:

from tkinter import  *

# main window configuration
root=Tk()
root.geometry("1000x1000")

root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
# main window frames

topmenu=Frame(root,padx=5,pady=5)
display=Frame(root,padx=5,pady=5)
keyboard=Frame(root,padx=5,pady=5,bg="red")

topmenu.grid(row=0,column=0)
display.grid(row=1,column=0)
keyboard.grid(row=2,column=0)




# topmenu widgets

# display widgets

#keyboard widgets
 #  prima riga
percentagebutton=Button(keyboard,padx=5,pady=5,text="%")
squarerootbutton=Button(keyboard,padx=5,pady=5,text="√x")
squarebutton=Button(keyboard,padx=5,pady=5,text="x²")
inversebutton=Button(keyboard,padx=5,pady=5,text="1/x")

percentagebutton.grid(row=0,column=0,sticky='EWNS')
squarerootbutton.grid(row=0,column=1,sticky='EWNS')
squarebutton.grid(row=0,column=2,sticky='EWNS')
inversebutton.grid(row=0,column=3,sticky='EWNS')

#   seconda riga
resetbutton=Button(keyboard,padx=5,pady=5,text="C")
deletebutton=Button(keyboard,padx=5,pady=5,text="←")
divisionbutton=Button(keyboard,padx=5,pady=5,text="÷")


resetbutton.grid(row=1,column=0,columnspan=2,sticky='EWNS')
deletebutton.grid(row=1,column=2,sticky='EWNS')
divisionbutton.grid(row=1,column=3,sticky='EWNS')


#   terza riga

sevenbutton=Button(keyboard,padx=5,pady=5,text="7")
eightbutton=Button(keyboard,padx=5,pady=5,text="8")
ninebutton=Button(keyboard,padx=5,pady=5,text="9")
moltiplicationbutton=Button(keyboard,padx=5,pady=5,text="X")

sevenbutton.grid(row=2,column=0,sticky='EWNS')
eightbutton.grid(row=2,column=1,sticky='EWNS')
ninebutton.grid(row=2,column=2,sticky='EWNS')
moltiplicationbutton.grid(row=2,column=3,sticky='EWNS')
#   quarta riga
fourbutton=Button(keyboard,padx=5,pady=5,text="4")
fivebutton=Button(keyboard,padx=5,pady=5,text="5")
sixbutton=Button(keyboard,padx=5,pady=5,text="6")
minusbutton=Button(keyboard,padx=5,pady=5,text="-")

fourbutton.grid(row=3,column=0,sticky='EWNS')
fivebutton.grid(row=3,column=1,sticky='EWNS')
sixbutton.grid(row=3,column=2,sticky='EWNS')
minusbutton.grid(row=3,column=3,sticky='EWNS')






#   quinta riga
onebutton=Button(keyboard,padx=5,pady=5,text="1")
twobutton=Button(keyboard,padx=5,pady=5,text="2")
threebutton=Button(keyboard,padx=5,pady=5,text="3")
plusbutton=Button(keyboard,padx=5,pady=5,text="+")

onebutton.grid(row=4,column=0,sticky='EWNS')
twobutton.grid(row=4,column=1,sticky='EWNS')
threebutton.grid(row=4,column=2,sticky='EWNS')
plusbutton.grid(row=4,column=3,sticky='EWNS')


#   sesta riga
signbutton=Button(keyboard,padx=5,pady=5,text="±")
zerobutton=Button(keyboard,padx=5,pady=5,text="0")
commabutton=Button(keyboard,padx=5,pady=5,text=",")
resultbutton=Button(keyboard,padx=5,pady=5,text="=")

signbutton.grid(row=5,column=0,sticky='EWNS')
zerobutton.grid(row=5,column=1,sticky='EWNS')
commabutton.grid(row=5,column=2,sticky='EWNS')
resultbutton.grid(row=5,column=3,sticky='EWNS')



root.mainloop()

这是我的计算器当前的图像:

另一张图片来说明

如何制作keyboard framebuttons调整大小以匹配根窗口的边框?


问题答案:

问题 :Tkinter按钮使用网格扩展

TkDocs:网格几何管理器处理大小调整

您只配置root增长:

root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)

如果有多余的空间 ,您还必须配置keyboard框架以使其 增长添加

keyboard.grid_columnconfigure(0, weight=1)
keyboard.grid_rowconfigure(0, weight=1)

您必须配置 每一个 keyboard row/columnweight=1

您没有设置keyboard扩展框架

keyboard.grid(row=0,column=0)

改成

keyboard.grid(row=0,column=0, sticky=NSEW)

工作示例:

root=Tk()
root.geometry("500x500")
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
keyboard=Frame(root,padx=5,pady=5,bg="red")
keyboard.grid(row=0,column=0, sticky=NSEW)

for row, row_buttons in enumerate([['%', '√x', 'x²', '1/x'], ["C", "←", "÷"]]):
    keyboard.grid_rowconfigure(row, weight=1)
    for col, text in enumerate(row_buttons):
        keyboard.grid_columnconfigure(col, weight=1)
        Button(keyboard, padx=5, pady=5, text=text).grid(row=row, column=col, sticky='EWNS')

在此处输入图片说明

使用Python测试:3.5-TkVersion:8.6



 类似资料:
  • 我正在用PythonTkinter编写一个GUI,其中有一个由大约24个按钮组成的网格,我使用循环(不是单独创建的)创建了该网格。有没有什么方法可以让我得到我按下的按钮的文本。 因为它在循环中,所以即使使用lambda,回调函数也帮不了我。我不想写单独的代码,当按下每个不同的按钮时会发生什么。我只需要知道相应按钮的文本,这样我就可以启动另一个只处理该文本的通用函数。 附言:我可以做同样的任务,但是

  • 我是Python新手,对GUI编程更是新手。 我有一个按钮和两个旋转箱,我想在点击开始按钮后禁用它们。我在谷歌上搜索了5种禁用Tkinter按钮的不同方法,但似乎都不起作用。理论上,旋转箱应该以同样的方式被禁用,但我只是运气不好。对整个图形用户界面感到非常沮丧。 和是两个spinbox 正如您所看到的,我尝试对按钮使用,但它不起作用。我见过各种各样的代码,在所有的大写字母、大写字母和不同的引号中都

  • Button 控件是 Tkinter 中常用的窗口部件之一,同时也是实现程序与用户交互的主要控件。通过用户点击按钮的行为来执行回调函数,是 Button 控件的主要功用。首先自定义一个函数或者方法,然后将函数与按钮关联起来,最后,当用户按下这个按钮时,Tkinter 就会自动调用相关函数。 按钮控件使用起来非常简单,它同样可以包含文本、图像、位图,并通过 参数回调函数。当然按钮也并非一定要执行回调

  • 问题内容: 我不断收到以下错误:AttributeError:’NoneType’对象没有属性’configure’ 问题答案: 执行时,分配给该命令的结果是命令的结果, 而不是 对创建的对象的引用。 您需要在打包/网格化之前分配变量。它看起来应该像这样:

  • 我有一个2x2的按钮网格。现在我想把按钮缩小到200x200,把每个按钮放在一个容器中作为一个空格,然后把每个按钮放在各自容器的中心。 在我的图片中,我只缩小了顶部的两个按钮,以便您可以看到页面上的间距。我怎样才能从第一张图片转到第二张图片,这是Photoshop制作的? 文件:app.js 文件:index.html

  • 本文向大家介绍python-tkinter之按钮的使用,开关方法,包括了python-tkinter之按钮的使用,开关方法的使用技巧和注意事项,需要的朋友参考一下 具体参考哪位大佬的,记不太清楚了。 直接上代码,大体逻辑是这样的。 说一说我遇到的问题: 我想让这个开始按钮,点击变成开始,再次点击变成结束。 使用上面的方法是失败的,我发现self.B打印出来为空。 是因为后面调用了grid方法导致的