tkintrt.Button位置试炼——计算器“键盘”

pack、grid、place由你摔摆,pack简单易用;grid网格安排;place最最随意,位置最细粒度随你捏拿。


(笔记模板由python脚本于2024年10月21日 19:21:44创建,本篇笔记适合喜欢python喜欢GUI的coder翻阅)


【学习的细节是欢悦的历程】


  自学并不是什么神秘的东西,一个人一辈子自学的时间总是比在学校学习的时间长,没有老师的时候总是比有老师的时候多。
            —— 华罗庚


等风来,不如追风去……


pack、grid、place由你摔摆
tkintrt.Button试炼
(pack简单grid网格排布place粒度拿捏)


本文质量分:

96 96 96

本文地址: 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组件


  tkinterButton组件是Tkinter GUI库中的一个基本控件,用于创建按钮。


以下是Button组件的一些常用属性,可以通过这些属性来定制按钮的外观和行为:

  1. text:按钮上显示的文本。
  2. command:当按钮被点击时调用的函数。
  3. width:按钮的宽度(单位是字符)
  4. height:按钮的高度(单位是字符)
  5. state:按钮的状态,可以是NORMALACTIVEDISABLED之一。
  6. bg:按钮的背景颜色。
  7. fg:按钮的前景色,即按钮上文字的颜色。
  8. font:按钮中文本的字体样式。
  9. image:按钮上显示的图像。
  10. compound:如果按钮同时包含文本和图像,这个属性决定了它们是如何显示的,可以是BOTTOMCENTERLEFTRIGHTTOP
  11. cursor:当鼠标移动到按钮上时,鼠标光标的样式。
  12. activebackground:当按钮被按下时显示的背景颜色。
  13. activeforeground:当按钮被按下时显示的前景色。
  14. highlightbackground:当按钮没有焦点时的边框颜色。
  15. highlightcolor:当按钮有焦点时的边框颜色。
  16. highlightthickness:边框的宽度。
  17. padx:按钮文本水平方向上的额外空间(内边距)
  18. pady:按钮文本垂直方向上的额外空间(内边距)
  19. relief:按钮的边框样式,可以是FLATSUNKENRAISEDGROOVERIDGE之一。
  20. underline:指定按钮文本中哪个字符下应该有下划线,通常用于创建快捷键。
  21. anchor:文本或图像在按钮中的位置,可以是NNEESESSWWNWCENTER之一。

下面是一个简单的示例,创建一个带有文本的按钮:

#!/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(
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦幻精灵_cq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值