当前位置: 首页 > news >正文

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

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 CSDN主页、My HOT博、My Python 学习个人备忘录
  • 好文力荐、 老齐教室
等风来,不如追风去……


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


本文质量分:

96 96 96

本文地址: https://blog.csdn.net/m0_57158496/article/details/143125570

CSDN质量分查询入口:http://www.csdn.net/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 tkroot = 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(x=520, y=1125)# 00 0 . =
button = tk.Button(text='00', width=button_width, height=button_height)
button.place(x=55, y=1260)
button = tk.Button(text='0', width=button_width, height=button_height)
button.place(x=210, y=1260) 
button = tk.Button(text='.' , width=button_width, height=button_height)
button.place(x=365, y=1260)
button = tk.Button(text='=' , width=button_width, height=button_height)
button.place(x=520, y=1260)root.mainloop()

  • 代码运行效果
    在这里插入图片描述



回页目录


4、精致“面板”


4.1 错版


python 脚本

import tkinter as tkroot = tk.Tk()
root.title('计算器键盘')
button_width = 4
button_height = 2
font_size = 10
custom_font = ('Arial', font_size)# C % × ÷
button = tk.Button(text='C', width=button_width, height=button_height, bg="lightgray", font=custom_font)
button.place(x=55, y=780)
button = tk.Button(text='%', width=button_width, height=button_height, fg="gray", font=custom_font)
button.place(x=210, y=780) 
button = tk.Button(text='del' , width=button_width, height=button_height, fg="gray", font=custom_font)
button.place(x=365, y=780)
button = tk.Button(text='÷' , width=button_width, height=button_height, fg="gray", font=custom_font)
button.place(x=520, y=780)# 7 8 9 ×
button = tk.Button(text='7', width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=55, y=900)
button = tk.Button(text='8', width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=210, y=900) 
button = tk.Button(text='9' , width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=365, y=900)
button = tk.Button(text='×' , width=button_width, height=button_height, fg="gray", font=custom_font)
button.place(x=520, y=900)# 6 5 4 -
button = tk.Button(text='6', width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=55, y=1020)
button = tk.Button(text='5', width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=210, y=1020) 
button = tk.Button(text='4' , width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=365, y=1020)
button = tk.Button(text='-' , width=button_width, height=button_height, fg="gray", font=custom_font)
button.place(x=520, y=1020)# 3 2 1 +
button = tk.Button(text='3', width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=55, y=1140)
button = tk.Button(text='2', width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=210, y=1140) 
button = tk.Button(text='1' , width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=365, y=1140)
button = tk.Button(text='+' , width=button_width, height=button_height, fg="gray", font=custom_font)
button.place(x=520, y=1140)# 00 0 . =
button = tk.Button(text='00', width=button_width, height=button_height, fg="gray", font=custom_font)
button.place(x=55, y=1260)
button = tk.Button(text='0', width=button_width, height=button_height, fg="gray", font=custom_font)
button.place(x=210, y=1260) 
button = tk.Button(text='.' , width=button_width, height=button_height, fg="gray", font=custom_font)
button.place(x=365, y=1260)
button = tk.Button(text='=' , width=button_width, height=button_height, bg="lightgray", font=custom_font)
button.place(x=520, y=1260)root.mainloop()

  • 效果
    在这里插入图片描述
    😂😂现在才发现,我居然把字符整错乱了。😂😂😂

4.2 修正版

调整代码脚本

import tkinter as tkroot = tk.Tk()
root.title('计算器键盘')
button_width = 4
button_height = 2
font_size = 10
custom_font = ('Arial', font_size)# C % × ÷
button = tk.Button(text='C', width=button_width, height=button_height, bg="lightgray", fg='red', font=custom_font)
button.place(x=55, y=780)
button = tk.Button(text='%', width=button_width, height=button_height, fg="blue", font=custom_font)
button.place(x=210, y=780) 
button = tk.Button(text='del' , width=button_width, height=button_height, fg="blue", font=custom_font)
button.place(x=365, y=780)
button = tk.Button(text='÷' , width=button_width, height=button_height, fg="blue", font=custom_font)
button.place(x=520, y=780)# 7 8 9 ×
button = tk.Button(text='7', width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=55, y=900)
button = tk.Button(text='8', width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=210, y=900) 
button = tk.Button(text='9' , width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=365, y=900)
button = tk.Button(text='×' , width=button_width, height=button_height, fg="blue", font=custom_font)
button.place(x=520, y=900)# 4 5 6 -
button = tk.Button(text='4', width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=55, y=1020)
button = tk.Button(text='5', width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=210, y=1020) 
button = tk.Button(text='6' , width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=365, y=1020)
button = tk.Button(text='-' , width=button_width, height=button_height, fg="blue", font=custom_font)
button.place(x=520, y=1020)# 1 2 3 +
button = tk.Button(text='1', width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=55, y=1140)
button = tk.Button(text='2', width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=210, y=1140) 
button = tk.Button(text='3' , width=button_width, height=button_height, bg='lightgray', font=custom_font)
button.place(x=365, y=1140)
button = tk.Button(text='+' , width=button_width, height=button_height, fg="blue", font=custom_font)
button.place(x=520, y=1140)# 00 0 . =
button = tk.Button(text='00', width=button_width, height=button_height, fg="blue", font=custom_font)
button.place(x=55, y=1260)
button = tk.Button(text='0', width=button_width, height=button_height, fg="blue", font=custom_font)
button.place(x=210, y=1260) 
button = tk.Button(text='.' , width=button_width, height=button_height, fg="blue", font=custom_font)
button.place(x=365, y=1260)
button = tk.Button(text='=' , width=button_width, height=button_height, bg="lightgray", fg='red', font=custom_font)
button.place(x=520, y=1260)root.mainloop()

  • 这才正版了😋
    在这里插入图片描述

4.3 .Button之text属性修饰


  Tkinter的Button组件的text属性字体大小可以通过使用font属性来自定义。font属性可以指定一个字体对象,这个对象定义了字体的样式、大小和族(family)。以下是如何设置Button文本字体大小的示例:


import tkinter as tk# 创建主窗口
root = tk.Tk()
root.title("Custom Font Size Example")# 创建一个字体对象,指定字体大小
font_size = 16  # 字体大小
custom_font = ("Arial", font_size)# 创建一个按钮,使用自定义字体大小
button = tk.Button(root, text="Click Me", font=custom_font)
button.pack(pady=20)  # 添加一些垂直填充# 启动事件循环
root.mainloop()

  在这个例子中,我们首先定义了一个字体对象custom_font,其中包含了字体族(“Arial”)和字体大小(16)。然后我们将这个字体对象传递给Buttonfont属性,这样按钮的文本就会使用我们指定的字体大小。

  你可以根据需要修改"Arial"为你想要的任何字体族,并调整font_size为你想要的字体大小。如果你还想设置字体的粗细或斜体等样式,可以在字体对象中添加这些参数,例如:("Arial", font_size, "bold", "italic")



回页目录


5、tkinter.Button位置控制


  在Tkinter中,使用布局管理器(pack、grid、place) 控制tk.Button在窗口中的位置。Tkinter 提供了三种主要的布局管理器:packgridplace


通常有以下几种方法:

5.1 使用 pack 布局管理器

import tkinter as tkroot = tk.Tk()# 创建一个按钮
button = tk.Button(root, text="点击我")# 使用 pack 布局管理器,并设置位置参数
button.pack(side=tk.LEFT)  # 将按钮放置在窗口的左侧
# 或者
button.pack(side=tk.RIGHT)  # 将按钮放置在窗口的右侧
# 或者
button.pack(side=tk.TOP)  # 将按钮放置在窗口的顶部
# 或者
button.pack(side=tk.BOTTOM)  # 将按钮放置在窗口的底部root.mainloop()

5.2 使用 grid 布局管理器

import tkinter as tkroot = tk.Tk()# 创建一个按钮
button = tk.Button(root, text="点击我")# 使用 grid 布局管理器,并设置行列参数
button.grid(row=0, column=0)  # 将按钮放置在第0行第0列root.mainloop()

5.3 使用 place 布局管理器

import tkinter as tkroot = tk.Tk()# 创建一个按钮
button = tk.Button(root, text="点击我")# 使用 place 布局管理器,并设置绝对位置参数
button.place(x=50, y=100)  # 将按钮放置在窗口的(50, 100)位置root.mainloop()

5.4 布局管理器说明


  • pack: 简单地将组件放置在窗口中的某个位置。可以使用side参数指定位置,还可以使用fillexpand等参数进一步控制布局。
  • grid: 将窗口分割成一个二维的表格,通过rowcolumn参数指定组件的位置。sticky参数可以用来控制组件在单元格中的对齐方式。
  • place: 允许你指定组件的确切位置和大小,通过xy参数来设置组件的绝对位置。

  每种布局管理器都有其用途和特点,通常pack是最简单的,grid适用于更复杂的布局,而place提供了最大的灵活性,但通常不推荐用于响应式布局,因为它不会自动调整组件大小和位置以适应窗口大小变化。


  我试炼tk.Button,用的.place,感觉它更“得心应手”😎



回页目录


6、代码精简


  我的代码实现太过“重复”,所以我要“函数重用”,让脚本行数缩减。


精简代码脚本


代码正在努力准备ing……



回页首


上一篇:  代码“300x200”中的“x”(一般编程代码中对“块儿”大小描述的“x”,是小写字母“x”而不是运算符“×”)
下一篇: 



我的HOT博:

  本次共计收集 311 篇博文笔记信息,总阅读量43.82w。数据于2024年03月22日 00:50:22完成采集,用时6分2.71秒。阅读量不小于6.00k的有 7 7 7篇。

  • 001
    标题:让QQ群昵称色变的神奇代码
    (浏览阅读 5.9w )
    地址:https://blog.csdn.net/m0_57158496/article/details/122566500
    点赞:25 收藏:86 评论:17
    摘要:让QQ昵称色变的神奇代码。
    首发:2022-01-18 19:15:08
    最后编辑:2022-01-20 07:56:47

  • 002
    标题:Python列表(list)反序(降序)的7种实现方式
    (浏览阅读 1.1w )
    地址:https://blog.csdn.net/m0_57158496/article/details/128271700
    点赞:8 收藏:35 评论:8
    摘要:Python列表(list)反序(降序)的实现方式:原址反序,list.reverse()、list.sort();遍历,全数组遍历、1/2数组遍历;新生成列表,resersed()、sorted()、负步长切片[::-1]。
    首发:2022-12-11 23:54:15
    最后编辑:2023-03-20 18:13:55

  • 003
    标题:pandas 数据类型之 DataFrame
    (浏览阅读 9.7k )
    地址:https://blog.csdn.net/m0_57158496/article/details/124525814
    点赞:7 收藏:36 
    摘要:pandas 数据类型之 DataFrame_panda dataframe。
    首发:2022-05-01 13:20:17
    最后编辑:2022-05-08 08:46:13

  • 004
    标题:个人信息提取(字符串)
    (浏览阅读 8.2k )
    地址:https://blog.csdn.net/m0_57158496/article/details/124244618
    点赞:2 收藏:15 
    摘要:个人信息提取(字符串)_个人信息提取python。
    首发:2022-04-18 11:07:12
    最后编辑:2022-04-20 13:17:54

  • 005
    标题:Python字符串居中显示
    (浏览阅读 7.6k )
    地址:https://blog.csdn.net/m0_57158496/article/details/122163023
    评论:1

  • 006
    标题:罗马数字转换器|罗马数字生成器
    (浏览阅读 7.5k )
    地址:https://blog.csdn.net/m0_57158496/article/details/122592047
    摘要:罗马数字转换器|生成器。
    首发:2022-01-19 23:26:42
    最后编辑:2022-01-21 18:37:46

  • 007
    标题:回车符、换行符和回车换行符
    (浏览阅读 6.0k )
    地址:https://blog.csdn.net/m0_57158496/article/details/123109488
    点赞:2 收藏:3 
    摘要:回车符、换行符和回车换行符_命令行回车符。
    首发:2022-02-24 13:10:02
    最后编辑:2022-02-25 20:07:40


推荐条件 阅读量突破6.00k
(更多热博,请点击蓝色文字跳转翻阅)

  • 截屏图片
    在这里插入图片描述
      (此文涉及ChatPT,曾被csdn多次下架,前几日又因新发笔记被误杀而落马。躺“未过审”还不如回收站,回收站还不如永久不见。😪值此年底清扫,果断移除。留此截图,以识“曾经”。2023-12-31)



回页首


老齐漫画头像

精品文章:

  • 好文力荐:齐伟书稿 《python 完全自学教程》 Free连载(已完稿并集结成书,还有PDF版本百度网盘永久分享,点击跳转免费🆓下载。)
  • OPP三大特性:封装中的property
  • 通过内置对象理解python'
  • 正则表达式
  • python中“*”的作用
  • Python 完全自学手册
  • 海象运算符
  • Python中的 `!=`与`is not`不同
  • 学习编程的正确方法

来源:老齐教室


◆ Python 入门指南【Python 3.6.3】


好文力荐:

  • 全栈领域优质创作者——[寒佬](还是国内某高校学生)博文“非技术文—关于英语和如何正确的提问”,“英语”和“会提问”是编程学习的两大利器。
  • 【8大编程语言的适用领域】先别着急选语言学编程,先看它们能干嘛
  • 靠谱程序员的好习惯
  • 大佬帅地的优质好文“函数功能、结束条件、函数等价式”三大要素让您认清递归

CSDN实用技巧博文:

  • 8个好用到爆的Python实用技巧
  • python忽略警告
  • Python代码编写规范
  • Python的docstring规范(说明文档的规范写法)


http://www.mrgr.cn/news/55621.html

相关文章:

  • 什么是 SQL 注入攻击?如何防止 SQL 注入?
  • Redis 常用指令详解
  • Springboot项目
  • 滚雪球学Redis[7.0讲]:Redis在Web应用中的会话管理:实现、优化与安全性!
  • 【STM32项目_2_基于STM32的宠物喂食系统】
  • 【MySQL】提高篇—复杂查询:多表连接(INNER JOIN、LEFT JOIN、RIGHT JOIN、FULL JOIN)
  • MySQL—CRUD—进阶—(二) (ಥ_ಥ)
  • 基于springboot的网上服装商城推荐系统的设计与实现
  • BitNet: Scaling 1-bit Transformers for Large Language Models
  • 数据库中常用的函数及函数应用
  • FCITX5的一些小命令
  • Spring Boot:如何实现JAR包的直接运行
  • 静态代码块为什么不能放在构造函数中
  • 在C++中比大小
  • 嵌入式开发学习——c语言完结
  • 10.21 IO进程直接的通信
  • .mkp勒索病毒攻击全攻略:防护、数据恢复与安全建议
  • LC:动态规划-买卖股票
  • IPv4头部和IPv6头部
  • Lua中的goto语句
  • ZYNQ:流水灯实验
  • .net framework3.5sp1runtime组件怎么开启
  • Python Web 框架中 Django 框架
  • Java面试题五
  • AB包资源管理器
  • CISP/NISP二级练习题-第一卷