Python GUI界面编程初步 03 - Tkinter基础设计案例
作者:互联网
1 设计:
We'll create a simple GUI tool to convert a distance in feet to the equivalent distance in meters. If we were to sketch this out, it might look something like this:
做一个长度的转化小程序UI,公制和英制进行转化。
设计这个,可以划分为三行三列的样子。
The next thing we need to do is look at the layout. The widgets that we've included seem to be naturally divided into a grid with three columns and three rows. In terms of layout, things seem to naturally divide into three columns and three rows, as illustrated below:
The layout of our user interface, which follows a 3 x 3 grid.
Python之禅 by Tim Peters
优美胜于丑陋(Python 以编写优美的代码为目标)
明了胜于晦涩(优美的代码应当是明了的,命名规范,风格相似)
简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现)
复杂胜于凌乱(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁)
扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套)
间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题)
可读性很重要(优美的代码是可读的)
即便假借特例的实用性之名,也不可违背这些规则(这些规则至高无上)
基础的顺序设定代码
from tkinter import *
from tkinter import ttk
def calculate(*args):
try:
value = float(feet.get())
meters.set(int(0.3048 * value * 10000.0 + 0.5)/10000.0)
except ValueError:
pass
root = Tk() #设置主程序窗口
root.title("Feet to Meters") #设置窗口标题
mainframe = ttk.Frame(root, padding="3 3 12 12") #创建一个frame部件,3行3列,12宽
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
feet = StringVar() #创建一个输入框部件,并放置到屏幕上
feet_entry = ttk.Entry(mainframe, width=7, textvariable=feet) # mainframe 是parent,就是部件要放的地方
feet_entry.grid(column=2, row=1, sticky=(W, E))#放置一个网格,并决定放置位置
meters = StringVar()
ttk.Label(mainframe, textvariable=meters).grid(column=2, row=2, sticky=(W, E))
ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=3, row=3, sticky=W)
ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)
ttk.Label(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="meters").grid(column=3, row=2, sticky=W)
for child in mainframe.winfo_children():
child.grid_configure(padx=5, pady=5) #调整版本间隙
feet_entry.focus() #焦点放在entry 输入框
root.bind("<Return>", calculate) #回车键开始计算
root.mainloop() # 开始事件监听
将Main封装的代码:
from tkinter import *
from tkinter import ttk
class FeetToMeters:
def __init__(self, root):
root.title("Feet to Meters")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
self.feet = StringVar()
feet_entry = ttk.Entry(mainframe, width=7, textvariable=self.feet)
feet_entry.grid(column=2, row=1, sticky=(W, E))
self.meters = StringVar()
ttk.Label(mainframe, textvariable=self.meters).grid(column=2, row=2, sticky=(W, E))
ttk.Button(mainframe, text="Calculate", command=self.calculate).grid(column=3, row=3, sticky=W)
ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)
ttk.Label(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="meters").grid(column=3, row=2, sticky=W)
for child in mainframe.winfo_children():
child.grid_configure(padx=5, pady=5)
feet_entry.focus()
root.bind("<Return>", self.calculate)
def calculate(self, *args):
try:
value = float(self.feet.get())
self.meters.set(int(0.3048 * value * 10000.0 + 0.5)/10000.0)
except ValueError:
pass
root = Tk()
FeetToMeters(root)
root.mainloop()
程序说明:
from tkinter import * # 标准绑定到Tk , load Tk lib
from tkinter import ttk # ttk 是tkinter的子模块,执行"themed widgets"
参考:
TkDocs Tutorial - A First (Real) Example
Python 基础语法 | 菜鸟教程 (runoob.com)
(8条消息) Python语言说明_dc90000的博客-CSDN博客
标签:03,Python,GUI,ttk,mainframe,grid,root,feet,row 来源: https://blog.csdn.net/yellow_hill/article/details/118443763