当前位置:网站首页>用tkinter做一个简单图形界面
用tkinter做一个简单图形界面
2022-07-06 16:59:00 【一只大鸽子】
tkinter简介
tkinter是Python自带的一个GUI包。优缺点非常明显:
优点:简单、快速、无需安装
缺点:界面复古,缺少对一些复杂功能的支持
(注意,Python2的tkinter名称为Tkinter,我们不讨论它)
启动tkinter
在命令行输入
python -m tkinter
就会弹出一个tkinter窗口:
最上面首先是版本是8.6,点击click me!貌似什么都不会发生,点击QUIT就可以退出。
下面介绍tkinter的简单用法,更多的可以去GUI是什么 (biancheng.net)或
Python GUI 编程(Tkinter) | 菜鸟教程 (runoob.com) 或Tkinter 8.5 reference: a GUI for Python (tkdocs.com)学习。
tkinter HelloWorld
使用tkinter创建一个窗口,标题名称为Hello,world。
# -*- coding:utf-8 -*-
import tkinter as tk
# 调用Tk()创建主窗口
root_window =tk.Tk()
# 给主窗口起一个名字,也就是窗口的名字
root_window.title('Hello,world')
#设置窗口大小 450x300
root_window.geometry('450x300')
#开启主循环,让窗口处于显示状态
root_window.mainloop()
导入:tkinter
创建主窗口: root_window =tk.Tk()
设置窗口属性(标题、大小):
root_window.title(‘Hello,world’)
root_window.geometry(‘450x300’)
开启主循环
root_window.mainloop()
tkiner 身高计算器
现在我们来做一个有功能的tkinter程序。
最终效果如下:我们输入身高,点击计算。然后程序通过计算,得到我们的身高并显示出来。
那么我们在上面Helloworld程序的基础上开始制作。首先,我们把窗口名改成“身高计算器”。
root_window.title('身高计算器')
然后我们要把按钮,提示信息放上去。
# root_window.geometry('450x300')下面添加...
# 设置完窗口后,添加组件
tk.Label(root_window, text="请输入身高").pack()
cms= StringVar()
tk.Entry(root_window, width=7, textvariable=cms).pack()
tk.Label(root_window, text="cm").pack()
tk.Label(root_window, text="你的身高是").pack()
result = StringVar()
tk.Label(root_window, textvariable=result).pack()
tk.Label(root_window, text="cm").pack()
B = tk.Button(root_window, text="计算")
B.pack()
我们主要用到了三种组件,tk.Label
、tk.Entry
和tk.Button
,分别表示文本标签、输入框、按钮。创建完组件后需要调用.pack()方法放置,否则不会出现在界面上。
说明:以这个tk.Label为例。参数需要传入它的父容器(这里是root_window),文字内容(text=“请输入身高”)。然后调用.pack()方法将它放在窗口上。
tk.Label(root_window, text="请输入身高").pack()
这个我们也可以让Label的文字是一个变量。下面我们先创建一个变量result = StringVar(),然后将result作为tk.Label的参数。
result = StringVar() tk.Label(root_window, textvariable=result).pack()
但是点击计算后,并不会有动作,我们还需要将点击按钮和功能绑定。
我们定义函数calculate
来实现功能,先获取输入的数据(cmt.get()),再设置输出文本的数据(result.set())。
def calculate(*args):
try:
value = float(cms.get())
result.set(value)
except ValueError:
pass
并且通过command=calculate 将calculate方法和点击按钮绑定。
B = tk.Button(root_window, text="计算",command=calculate)
完整代码:
# -*- coding:utf-8 -*-
import tkinter as tk
from tkinter import *
# 调用Tk()创建主窗口
root_window = tk.Tk()
# 给主窗口起一个名字,也就是窗口的名字
root_window.title('身高计算器')
# 设置窗口大小 450x300
root_window.geometry('450x300')
# root_window.geometry('450x300')下面添加...
# 设置完窗口后,添加组件
tk.Label(root_window, text="请输入身高").pack()
cms = StringVar()
tk.Entry(root_window, width=7, textvariable=cms).pack()
tk.Label(root_window, text="cm").pack()
tk.Label(root_window, text="你的身高是").pack()
result = StringVar()
tk.Label(root_window, textvariable=result).pack()
tk.Label(root_window, text="cm").pack()
def calculate(*args):
try:
value = float(cms.get())
result.set(value)
except ValueError:
pass
B = tk.Button(root_window, text="计算", command=calculate)
B.pack()
# 开启主循环,让窗口处于显示状态
root_window.mainloop()
除了通过.pack()放置组件,另一种更灵活的方式是.grid(row=r,column=0)方式。这种方式将界面作为网格,然后在网格上放置组件。
# 设置完窗口后,添加组件
tk.Label(root_window, text="请输入身高",).grid(row=0,column=0)
cms = StringVar()
tk.Entry(root_window, width=7, textvariable=cms).grid(row=0,column=1)
tk.Label(root_window, text="cm").grid(row=0,column=2)
tk.Label(root_window, text="你的身高是").grid(row=1,column=0)
result = StringVar()
tk.Label(root_window, textvariable=result).grid(row=1,column=1)
tk.Label(root_window, text="cm").grid(row=1,column=2)
def calculate(*args):
try:
value = float(cms.get())
result.set(value)
except ValueError:
pass
B = tk.Button(root_window, text="计算", command=calculate)
B.grid(row=3,column=3)
当然,如果你觉得字体太小,也可以在font参数设置,font是一个三元组( family, size, style),分别是(字体,大小,格式)。
tk.Label(root_window, text="请输入身高",font=("Courier", 24, "italic"),height=3).grid(row=0,column=0)
tkinter BMI计算器
稍作修改,我们就可以做出一个BMI计算器:
代码:
# -*- coding:utf-8 -*-
import tkinter as tk
from tkinter import *
# 调用Tk()创建主窗口
root_window = tk.Tk()
# 给主窗口起一个名字,也就是窗口的名字
root_window.title('BMI计算器')
# 设置窗口大小 450x300
root_window.geometry('450x300')
# root_window.geometry('450x300')下面添加...
# 设置完窗口后,添加组件
tk.Label(root_window, text="请输入身高cm",height=3).grid(row=0,column=0)
cms = StringVar()
tk.Entry(root_window, width=7, textvariable=cms).grid(row=0,column=1)
tk.Label(root_window, text="cm").grid(row=0,column=2)
tk.Label(root_window, text="请输体重kg",height=3).grid(row=1,column=0)
kg = StringVar()
tk.Entry(root_window, width=7, textvariable=kg).grid(row=1,column=1)
tk.Label(root_window, text="kg").grid(row=1,column=2)
tk.Label(root_window, text="你BMI是").grid(row=2,column=0)
result = StringVar()
tk.Label(root_window, textvariable=result).grid(row=2,column=1)
def calculate(*args):
try:
cm_ = float(cms.get())
kg_ = float(kg.get())
value_ = kg_ / ((cm_/100) **2)
value_ = value_.__round__(3)
result.set(value_)
except ValueError:
pass
B = tk.Button(root_window, text="计算", command=calculate)
B.grid(row=3,column=3)
# 开启主循环,让窗口处于显示状态
root_window.mainloop()
边栏推荐
- Mujoco Jacobi - inverse motion - sensor
- The programmer resigned and was sentenced to 10 months for deleting the code. Jingdong came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
- PXE server configuration
- Leecode brush question record sword finger offer 58 - ii Rotate string left
- Leecode brush questions record sword finger offer 44 A digit in a sequence of numbers
- alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
- Command line kills window process
- Quaternion attitude calculation of madgwick
- The difference between redirectto and navigateto in uniapp
- 陀螺仪的工作原理
猜你喜欢
C9高校,博士生一作发Nature!
智能运维应用之道,告别企业数字化转型危机
三维扫描体数据的VTK体绘制程序设计
509 certificat basé sur Go
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
英雄联盟|王者|穿越火线 bgm AI配乐大赛分享
工程师如何对待开源 --- 一个老工程师的肺腑之言
Data analysis course notes (III) array shape and calculation, numpy storage / reading data, indexing, slicing and splicing
37 page overall planning and construction plan for digital Village revitalization of smart agriculture
Understand the misunderstanding of programmers: Chinese programmers in the eyes of Western programmers
随机推荐
alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
What is web penetration testing_ Infiltration practice
Matlab learning notes
Explain in detail the implementation of call, apply and bind in JS (source code implementation)
深度学习之环境配置 jupyter notebook
[2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test
uniapp实现从本地上传头像并显示,同时将头像转化为base64格式存储在mysql数据库中
Understand the misunderstanding of programmers: Chinese programmers in the eyes of Western programmers
Alexnet experiment encounters: loss Nan, train ACC 0.100, test ACC 0.100
学习光线跟踪一样的自3D表征Ego3RT
Cross-entrpy Method
2022/2/10 summary
How to judge whether an element in an array contains all attribute values of an object
Compilation of kickstart file
Clipboard management tool paste Chinese version
Interface master v3.9, API low code development tool, build your interface service platform immediately
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
Mujoco finite state machine and trajectory tracking
Things like random
Zynq transplant ucosiii