PythonGUI编程:tkinter初学者入门指南——几何布局...

云课堂学Python 2024-12-10 03:41:36

kinter 可以使用几何布局管理器来组织窗口上的小部件。Tkinter 支持三种几何布局管理器:

packgridplace

在本文中,将介绍 Tkinter 几何布局管理器 Place,使用 (x, y) 坐标系在其容器内精确定位小部件。

relx 、rely

Place 几何布局管理器提供绝对定位和相对定位。

绝对定位由 x 和 y 选项指定。

相对位置由 relx 和 rely 选项指定。

import tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('几何布局管理器演示')button1 = tk.Button(root, text="x=100, y=100", width=20, bg='green')button1.place(x=100, y=100)button2 = tk.Button(root, text="relx=0.5, rely=0.5", width=20, bg='green')button2.place(relx=0.5, rely=0.5)root.mainloop()

relwidth 、relheight

以像素为单位设置窗口小部件的绝对宽度和高度,请使用 width 和 height 选项。相对宽度和高度,请使用 relwidth 和 relheight 选项。

import tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('几何布局管理器演示')button1 = tk.Button(root, text="绝对宽高", bg='red')button1.place(x=100, y=100, width=100, height=50)button2 = tk.Button(root, text="相对宽高", bg='green')button2.place(relx=0.5, rely=0.5, relwidth=0.5, relheight=0.5)root.mainloop()

relwidth``relheight 允许你以相对于父容器的宽度和高度来设置小组件的宽度和高度。意味着这些属性值会根据父容器的尺寸动态调整,从而使得组件的尺寸能够适应父组件的变化。

relwidth``relheight 取值范围 0 ~ 1。

anchor

anchor 参数确定小组件的哪个部分位于给定坐标处。

可以使用以下有效值:

N: 北或顶部中心。S: 南或底部中心。E: 东或右侧居中。W: 西或左侧居中。NW: 西北或左上角。NE: 东北或右上角。SE: 东南或右下角。SW: 西南或左下角。CENTER:中心。import tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('几何布局管理器演示')button1 = tk.Button(root, text="x=100, y=100", width=20, height=5, bg='red')button1.place(x=100, y=100)button2 = tk.Button(root, text="x=100, y=100", width=20, height=5, bg='green')button2.place(x=100, y=100, anchor=tk.CENTER)button3 = tk.Button(root, text="relx=0.5, rely=0.5", width=20, bg='red')button3.place(relx=0.5, rely=0.5, anchor=tk.CENTER)button4 = tk.Button(root, text="relx=0.5, rely=0.5", width=20, bg='green')button4.place(relx=0.5, rely=0.5)root.mainloop()

Place 布局综合示例import tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('几何布局管理器演示')root.resizable(False, False)var1 =tk.IntVar()var2 =tk.BooleanVar()left_frame = tk.Frame(root, width=220, height=390, bg='lightgrey')left_frame.place(x=5, y=5)right_frame = tk.Frame(root, width=365, height=390, bg='lightgrey')right_frame.place(x=230, y=5)tk.Label(left_frame, text="标签").place(relx=0.5, rely=0.02, anchor=tk.N)photo = tk.PhotoImage(file='ItYunKeTang.png')tk.Label(left_frame, image=photo).place(relx=0.5, rely=0.1, anchor=tk.N)tk.Button(left_frame, text="按钮", width=8).place(relx=0.2, rely=0.6, anchor=tk.CENTER)tk.Button(left_frame, text="按钮", width=8).place(relx=0.2, rely=0.7, anchor=tk.CENTER)tk.Button(left_frame, text="按钮", width=8).place(relx=0.2, rely=0.8, anchor=tk.CENTER)tk.Button(left_frame, text="按钮", width=8).place(relx=0.2, rely=0.9, anchor=tk.CENTER)labelframe1 = tk.LabelFrame(left_frame, text='复选框', width=100, height=80, bg='lightgrey')labelframe1.place(relx=0.7, rely=0.52, anchor=tk.N)tk.Checkbutton(labelframe1, text='选项1', variable=var1, bg='lightgrey').place(relx=0.4, rely=0.1, anchor=tk.N)tk.Checkbutton(labelframe1, text='选项2', variable=var2, bg='lightgrey').place(relx=0.4, rely=0.5, anchor=tk.N)labelframe2 = tk.LabelFrame(left_frame, text='单选框', width=100, height=80, bg='lightgrey')labelframe2.place(relx=0.7, rely=0.77, anchor=tk.N)tk.Radiobutton(labelframe2, text='选项1', variable=var2, value=1, bg='lightgrey').place(relx=0.4, rely=0.1, anchor=tk.N)tk.Radiobutton(labelframe2, text='选项2', variable=var2, value=2, bg='lightgrey').place(relx=0.4, rely=0.5, anchor=tk.N)photo2 = tk.PhotoImage(file='logo1.png')label = tk.Label(right_frame, image=photo2)label.place(relx=0.5, rely=0.02, anchor=tk.N)message ='''Python 提供了多个图形开发界面的库,几个常用 Python GUI 库如下:Tkinter:Tkinter 模块(Tk 接口)是Python 的标准 Tk GUI 工具包的接口.Tk 和 Tkinter 可以在大多数的 Unix平台下使用,同样可以应用在 Windows 和 Macintosh 系统里。Tk8.0 的后续版本可以实现本地窗口风格,并良好地运行在绝大多数平台中。wxPython:wxPython 是一款开源软件,是 Python 语言的一套优秀的 GUI 图形库,允许 Python 程序员很方便的创建完整的、功能健全的 GUI 用户界面。Jython:Jython 程序可以和 Java 无缝集成。除了一些标准模块,Jython 使用 Java 的模块。Jython 几乎拥有标准的Python 中不依赖于 C 语言的全部模块。比如,Jython 的用户界面将使用 Swing,AWT或者 SWT。Jython 可以被动态或静态地编译成 Java 字节码。'''text_box = tk.Text(right_frame, height=23, width=50)text_box.place(relx=0.5, rely=0.2, anchor=tk.N)text_box.insert('end', message)text_box.config(state='disabled')root.mainloop()

0 阅读:0