pack、grid、place由你摔摆,pack简单易用;grid网格安排;place最最随意,位置最细粒度随你捏拿。
(笔记模板由python脚本于2024年10月21日 19:21:44创建,本篇笔记适合喜欢python喜欢GUI的coder翻阅)
-
Python 官网:https://www.python.org/
-
Free:大咖免费“圣经”教程《 python 完全自学教程》,不仅仅是基础那么简单……
地址:https://lqpybook.readthedocs.io/
自学并不是什么神秘的东西,一个人一辈子自学的时间总是比在学校学习的时间长,没有老师的时候总是比有老师的时候多。
—— 华罗庚
- My 优快云主页、My HOT博、My Python 学习个人备忘录
- 好文力荐、 老齐教室
本文质量分:
本文地址: https://blog.youkuaiyun.com/m0_57158496/article/details/143125570
优快云质量分查询入口:http://www.youkuaiyun.com/qc
- ◆ tkintrt.Button位置试炼
-
- 1、计算器“面板”效果
- 2、tkinter.Button组件
- 3、普通面板
- 4、精致“面板”
-
- 4.1 错版
- 4.2 修正版
- 4.3 .Button之text属性修饰
- 5、tkinter.Button位置控制
-
- 5.1 使用 pack 布局管理器
- 5.2 使用 grid 布局管理器
- 5.3 使用 place 布局管理器
- 5.4 布局管理器说明
- 6、代码精简
◆ tkintrt.Button位置试炼
1、计算器“面板”效果
- “
Python编译器IDE”APP

最近淘到了一款app,她预置了好多“轮子”,其中也有tkinter。我在手机上也可以GUI了😎😎
虽然安卓设备还有一些限制,比如看不到root.title(‘我的窗口’)设定的标题,更有一些属性不可用,但至少可以在手机操练python GUI,也是大大的幸福。😋😋
效果截屏图片

初级版全部样式缺省

进阶版设置“底色”、字体
2、tkinter.Button组件
tkinter的Button组件是Tkinter GUI库中的一个基本控件,用于创建按钮。
以下是Button组件的一些常用属性,可以通过这些属性来定制按钮的外观和行为:
- text:按钮上显示的文本。
- command:当按钮被点击时调用的函数。
- width:按钮的宽度(单位是字符)。
- height:按钮的高度(单位是字符)。
- state:按钮的状态,可以是
NORMAL、ACTIVE、DISABLED之一。 - bg:按钮的背景颜色。
- fg:按钮的前景色,即按钮上文字的颜色。
- font:按钮中文本的字体样式。
- image:按钮上显示的图像。
- compound:如果按钮同时包含文本和图像,这个属性决定了它们是如何显示的,可以是
BOTTOM、CENTER、LEFT、RIGHT、TOP。 - cursor:当鼠标移动到按钮上时,鼠标光标的样式。
- activebackground:当按钮被按下时显示的背景颜色。
- activeforeground:当按钮被按下时显示的前景色。
- highlightbackground:当按钮没有焦点时的边框颜色。
- highlightcolor:当按钮有焦点时的边框颜色。
- highlightthickness:边框的宽度。
- padx:按钮文本水平方向上的额外空间(内边距)。
- pady:按钮文本垂直方向上的额外空间(内边距)。
- relief:按钮的边框样式,可以是
FLAT、SUNKEN、RAISED、GROOVE、RIDGE之一。 - underline:指定按钮文本中哪个字符下应该有下划线,通常用于创建快捷键。
- anchor:文本或图像在按钮中的位置,可以是
N、NE、E、SE、S、SW、W、NW、CENTER之一。
下面是一个简单的示例,创建一个带有文本的按钮:
#!/usr/bin/env python3.11
import tkinter as tk
root = tk.Tk()
button = tk.Button(root, text="点击我", command=lambda: print("按钮被点击了"))
button.pack()
root.mainloop()
你可以根据需要设置上述属性来定制按钮。
3、普通面板
初级“面板”脚本
import tkinter as tk
# 普通按钮
root = tk.Tk()
root.title('计算器键盘')
button_width = 5
button_height = 3
# C % × ÷
button = tk.Button(text='C', width=button_width, height=button_height)
button.place(x=55, y=720)
button = tk.Button(text='%', width=button_width, height=button_height)
button.place(x=210, y=720)
button = tk.Button(text='del' , width=button_width, height=button_height)
button.place(x=365, y=720)
button = tk.Button(text='÷' , width=button_width, height=button_height)
button.place(x=520, y=720)
# 7 8 9 ×
button = tk.Button(text='7', width=button_width, height=button_height)
button.place(x=55, y=855)
button = tk.Button(text='8', width=button_width, height=button_height)
button.place(x=210, y=855)
button = tk.Button(text='9' , width=button_width, height=button_height)
button.place(x=365, y=855)
button = tk.Button(text='×' , width=button_width, height=button_height)
button.place(x=520, y=855)
# 6 5 4 -
button = tk.Button(text='6', width=button_width, height=button_height)
button.place(x=55, y=990)
button = tk.Button(text='5', width=button_width, height=button_height)
button.place(x=210, y=990)
button = tk.Button(text='4' , width=button_width, height=button_height)
button.place(x=365, y=990)
button = tk.Button(text='-' , width=button_width, height=button_height)
button.place(x=520, y=990)
# 3 2 1 +
button = tk.Button(text='3', width=button_width, height=button_height)
button.place(x=55, y=1125)
button = tk.Button(text='2', width=button_width, height=button_height)
button.place(x=210, y=1125)
button = tk.Button(text='1' , width=button_width, height=button_height)
button.place(x=365, y=1125)
button = tk.Button(text='+' , width=button_width, height=button_height)
button.place(

最低0.47元/天 解锁文章
4386

被折叠的 条评论
为什么被折叠?



