前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >tk基础--标签、按钮

tk基础--标签、按钮

作者头像
xbhog
发布于 2019-10-28 10:29:49
发布于 2019-10-28 10:29:49
1.7K00
代码可运行
举报
文章被收录于专栏:开发技能乱炖开发技能乱炖
运行总次数:0
代码可运行

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_43908900/article/details/102750535

本节内容:

  1. 明白标签、按钮的使用
  2. 实现简单的点击界面

Tkinter 简称tk,在python中属于内置模块,不需要进行安装,可直接引用,import tkinter

一.

首先我们做一个如图所示的图形界面:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OdLqA2oJ-1572011460850)(C:\day_file\博客文件\GUI\image_GUi\tk1.jpg)]

它是由界面名(hello world)、标签(绿色、字符)组成,接下来我们实现上图;

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import tkinter as tk  #引包
#------------------------------------------------------------
# 创建一个对象
windows = tk.Tk()
# 设置标题
windows.title("hello world")
# 设置屏幕大小
windows.geometry("400x100")
#--------------------------------------------------------------
# 设置标签
l = tk.Label(windows, text="Hello World!", bg="green", font=("Arial", 12), width=15, height=2)
# 设置位置
l.pack()
# 设置循环
windows.mainloop()

程序流程:创建对象---->设置界面标题-----> 设置屏幕大小---->设置标签----->设置位置---->设置循环

注: 设置位置有三种方式布局,pack 是三种布局管理中最常用的. 另外两种布局需要精确指定控件具体的显示位置, 而 pack 布局可以指定相对位置, 精确的位置会由 pack 系统自动完成 。

进阶内容;做一个如下图所示:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CZeITFJJ-1572011460852)(C:\day_file\博客文件\GUI\image_GUi\tk2.1.jpg)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RWZfUIRi-1572011460853)(C:\day_file\博客文件\GUI\image_GUi\tk2.2.jpg)]

解释:当我点击按钮时,显示出"you click",再点击按钮时,无显示。基本元素与上面相同,下面我们实现一下

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import tkinker as tk
# 创建一个对象
windows = tk.Tk()
# 设置标题
windows.title("hello world")
# 设置屏幕大小
windows.geometry("400x100")
#设置字符串===》tk中需要设置自己类型的字符串
var = tk.StringVar()
#传入标签中
l = tk.Label(windows, textvariable=var, bg="green", font=("Arial", 12), width=15, height=2)
l.pack()

#设置判断
on_click = False
def Click():
    global  on_click
    if on_click == False:
        on_click = True
        #传入界面
        var.set("you click")
    else:
        on_click = False
        var.set("")
#设置点击按钮  command===》点击触发click函数
b = tk.Button(windows,text="Click on the",width=15,height=2,command=Click)
b.pack()

windows.mainloop()

流程解释:创建对象---->设置界面标题-----> 设置屏幕大小---->设置字符串传入标签----->设置位置---->设置触发函数---->点击按钮---->设置位置---->设置循环

注:在tk中将内容传入显示界面时需要特定的参数(textvariable),点击按钮时,设置触发函数(command),实现前端内容的变化.

未完待续…

初学者

容传入显示界面时需要特定的参数(textvariable),点击按钮时,设置触发函数(command),实现前端内容的变化.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/10/25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Javascript权威指南
The not-a-number value has one unusual feature in JavaScript: it does not compare equal to any other value, including itself.
全栈程序员站长
2021/12/27
8780
技术 | 用二进制算法加速神经网络
The original article is published on Nervana site: Accelerating Neural Networks with Binary Arithmetic. Please go to Nervana Homepage to learn more on Intel Nervana's deep learning technologies. At Nervana we are deeply interested in algorithmic and hard
计算机视觉研究院
2018/04/17
6870
技术 | 用二进制算法加速神经网络
Data Representation - Floating Point Numbers
In the last episode we talked about the data representation of integer, a kind of fixed-point numbers. Today we’re going to learn about floating-point numbers.
零式的天空
2022/03/28
4190
JSON5 格式标准 Data Exchange Format 官方文档 中英双语
The JSON5 Data Interchange Format is a proposed extension to JSON that aims to make it easier for humans to write and maintain by hand. It does this by adding some minimal syntax features directly from ECMAScript 5.1.JSON5 数据交换格式是一个提议的 JSON 扩展,旨在通过直接添加一些来自 ECMAScript 5.1 的最小语法特性,使人类更容易手动编写和维护。
WTSolutions
2025/03/30
1740
java.lang.NumberFormatException: Infinite or NaN原因之浮点类型除数为0结果探究
在对Double类型的数据进行计算操作,将结果转化为BigDecimal时抛出了下面的异常,进行了Debug才发现了问题原因,同时也暴露出了自己在一些基础知识上还有些欠缺。
翎野君
2023/05/12
5330
前端学习之NaN浅析
  在学习Java集合的时候遇到了Float.isNaN(float)函数,点进去一看就不理解了,函数实现如下:
Jetpropelledsnake21
2019/02/15
1.3K0
DAY34:阅读算术指令
5.4.1. Arithmetic Instructions Table 2 gives the throughputs of the arithmetic instructions that are
GPUS Lady
2018/06/22
6220
Array Broadcasting in Numpy
Let’s explore a more advanced concept in numpy called broadcasting. The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes. Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python. It does this without making needless copies of data and usually leads to efficient algorithm implementations. There are also cases where broadcasting is a bad idea because it leads to inefficient use of memory that slows computation. This article provides a gentle introduction to broadcasting with numerous examples ranging from simple to involved. It also provides hints on when and when not to use broadcasting.
狼啸风云
2019/09/25
5100
Array Broadcasting in Numpy
Source Code Reading for Vue 3: How does `hasChanged` work?
Hey, guys! The next generation of Vue has released already. There are not only the brand new composition API, much more powerful and flexible reactivity system, first-class render function, but also the natural performance with building off the modern browsers.
^_^肥仔John
2021/11/11
4160
谁是代码界3%的王者?- 第四题BigDecimal问题简单解读
提到“在Java代码界,有些陷阱外表看起来是个青铜实际上是王者,据说97%工程师会被“秒杀””
明明如月学长
2021/08/31
3530
谁是代码界3%的王者?- 第四题BigDecimal问题简单解读
基础野:细说浮点数
Brief                                 本来只打算理解JS中0.1 + 0.2 == 0.30000000000000004的原因,但发现自己对计算机的数字表示和运算十分陌生,于是只好恶补一下。  本篇我们一起来探讨一下基础——浮点数的表示方式和加减乘除运算。   在深入前有两点我们要明确的:   1. 在同等位数的情况下,浮点数可表示的数值范围比整数的大;   2. 浮点数无法精确表示其数值范围内的所有数值,只能精确表示可用科学计数法m*2e表示的数值而已;     
^_^肥仔John
2018/01/18
2.5K1
基础野:细说浮点数
常用的数学函数以及浮点数处理函数
在编程中我们总要进行一些数学运算以及数字处理,尤其是浮点数的运算和处理,这篇文章主要介绍C语言下的数学库。而其他语言中的数学库函数的定义以及最终实现也是通过对C数学库的调用来完成的,其内容大同小异,因此就不在这里介绍了。 C语言标准库中的math.h定义了非常多的数学运算和数字处理函数。这些函数大部分都是在C89标准中定义的,而有些C99标准下的函数我会特殊的说明,同时因为不同的编译器下的C标准库中有些函数的定义有差别,我也会分别的说明。
欧阳大哥2013
2018/08/22
2.7K0
常用的数学函数以及浮点数处理函数
C# 7.0简而言之 -- 02. C#基础 (1)
语句1里面计算了表达式(expression) 12 * 30, 并把结果保存到了本地变量x里面, x是整型类型.
solenovex
2018/05/03
1.1K0
C# 7.0简而言之 -- 02. C#基础 (1)
gcc x64 asm 内联汇编尝试
asm volatile(assembler template : output : input : clobber);
战神伽罗
2019/07/24
3K0
gcc x64 asm 内联汇编尝试
IEEE二进制浮点数算术标准(IEEE 754)
IEEE二进制浮点数算术标准(IEEE 754)是20世纪80年代以来最广泛使用的浮点数运算标准,为许多CPU与浮点运算器所采用。这个标准定义了表示浮点数的格式(包括负零-0)与反常值(denormal number)),一些特殊数值(无穷(Inf)与非数值(NaN)),以及这些数值的“浮点数运算符”;它也指明了四种数值舍入规则和五种例外状况(包括例外发生的时机与处理方式)。
用户7886150
2021/02/12
1.5K0
【递归】:[转]Dijkstra was right — recursion should not be difficult
“ …and discovered to my surprise that 10 % of my audience had the greatest difficulty in coping with the concept of recursive procedures. I was surprised because I knew that the concept of recursion was not difficult.” — Dijkstra’s keynote address of 1 March 1999
WEBJ2EE
2021/02/26
5620
【递归】:[转]Dijkstra was right — recursion should not be difficult
日更系列之c++的to_string的浮点数精度问题
做了一个根据搜索词计算embedding向量的服务,但是算法同学发现新服务打分精度变低了,原来能保存到小数点后16位的,现在打分只有小数点后6位。
mariolu
2022/03/05
3.1K0
Talking Head Anime from a Single Image将人脸表情移植到动漫表情中
原文地址https://pkhungurn.github.io/talking-head-anime/
水球喵子
2020/03/20
3K0
Talking Head Anime from a Single Image将人脸表情移植到动漫表情中
C++中检查浮点数值有效性
今天在项目中检查到一个bug,程序会在某些情况下崩溃,最终认定是计算一个比值时,被除数和除数均为零,导致计算结果是个无效值,在后面的代码将使用这个无效值时导致了崩溃。需要对这个结果是否有效进行判断。
用户7886150
2021/02/10
1.1K0
Under the Hood: NaN of JS
如果你还不确定这两题的答案的话,请仔细阅读本文。 这两题的答案不会直接解释,请从文章中寻找答案。
有赞coder
2020/08/25
1.6K0
Under the Hood: NaN of JS
相关推荐
Javascript权威指南
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验