python使用tkinter和ttkbootstrap制作UI界面(二)

2024-04-19 21:52

本文主要是介绍python使用tkinter和ttkbootstrap制作UI界面(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这次讲解UI界面常用的主键,延续上文的框架进行编写,原界面如下:
在这里插入图片描述

Combobox组件应用(下拉框)

        """Combobox组件"""global comvalue_operatorcomvalue_operator = tk.StringVar()value_operator = ttk.Combobox(self.frame_two, textvariable=comvalue_operator, width=15)value_operator["values"] = ["1", "2"]value_operator.current(0)value_operator.place(x=550, y=50)

效果如下:
在这里插入图片描述

ScrolledText组件应用(多行文本)

        """ScrolledText组件"""scr_explain = scrolledtext.ScrolledText(self.frame_two, font=('微软雅黑', 10), width=102, height=11)scr_explain.place(x=10, y=400)

效果如下:
在这里插入图片描述

Meter组件应用(圆形进度表)

        """Meter组件"""progress_bar = ttkbootstrap.Meter(master=self.frame_two,bootstyle="success",subtextstyle="warning",stripethickness=10,metertype="full",  # 将仪表显示为一个完整的圆形或半圆形(semi)# wedgesize=5, #设置弧周围的指示器楔形长度,如果大于 0,则此楔形设置为以当前仪表值为中心的指示器amounttotal=100,  # 仪表的最大值,默认100amountused=100,  # 仪表的当前值metersize=130,  # 仪表大小showtext=True,  # 指示是否在仪表上显示左、中、右文本标签interactive=False,  # 是否可以手动调节数字的大小# textleft='进度:',  # 插入到中心文本左侧的短字符串textright='%',textfont="-size 20",  # 中间数字大小# subtext="  ",subtext="完成",subtextfont="-size 8",  # 文本大小)progress_bar.place(x=550, y=150)

效果如下:
在这里插入图片描述

Treeview组件(数据表单)

        """Treeview组件"""tree_top = ["信道", "制式", "速率", "频宽"]frame = Frame(self.frame_two)frame.place(x=10, y=10, width=500)# 设置x轴和y轴的滚动条ybar = ttk.Scrollbar(frame, orient=VERTICAL)ybar.pack(side='right', fill='y')xbar = ttk.Scrollbar(frame, orient=tk.HORIZONTAL)xbar.pack(side='bottom', fill='x')# 创建表格self.tree_date = ttk.Treeview(frame, show='tree headings', height=15)self.tree_date.pack(side='left', fill='y')self.tree_date.configure(yscrollcommand=ybar.set, xscrollcommand=xbar.set)# 使用command参数来绑定treevievwybar.config(command=self.tree_date.yview)xbar.config(command=self.tree_date.xview)# 定义列date_columns = tree_topself.tree_date["columns"] = date_columns# 设置列宽度self.tree_date.column('#0', width=70, anchor="center", stretch=False)self.tree_date.column(tree_top[0], width=120, anchor="center")self.tree_date.column(tree_top[1], width=120, anchor="center")self.tree_date.column(tree_top[2], width=120, anchor="center")self.tree_date.column(tree_top[3], width=120, anchor="center")# 添加列名self.tree_date.heading("#0", text="场景")for j in date_columns:self.tree_date.heading(j, text=j)

效果如下:
在这里插入图片描述

Style风格应用(兼容老的参数设置)

        """Style使用"""style = ttk.Style()style.configure('custom.TButton', font=('宋体', 20), background='red')style.configure("My.TButton", font=('微软雅黑', 10), width=9, height=2)test_button_one = ttkbootstrap.Button(self.frame_two, text="测试按键一")test_button_one["style"] = "custom.TButton"test_button_one.place(x=750, y=50)test_button_two = ttkbootstrap.Button(self.frame_two, text="测试按键二")test_button_two["style"] = "My.TButton"test_button_two.place(x=750, y=150)

效果如下:
在这里插入图片描述
本次在ui_test.py新增代码,新增如下:

import ttkbootstrap
from tkinter import Framefrom ui_show.test_one import UiTestOne
from ui_show.test_two import UiTestTwoclass UiTest(object):def __init__(self):self.window = ttkbootstrap.Window()self.frame_one = Frame()self.frame_two = Frame()def main_interface(self):"""测试主界面"""# 设置标题self.window.title('测试UI界面')# 屏幕的尺寸大小sw = self.window.winfo_screenwidth()sh = self.window.winfo_screenheight()# 设置UI界面的高度和宽度ww = 1000wh = 700# 窗口居中设置x = (sw - ww) / 2y = (sh - wh) / 2self.window.geometry("%dx%d+%d+%d" % (ww, wh, x, y))# 设置窗口是否可以变化长宽,默认可变self.window.resizable(width=False, height=False)"""多窗口设置"""all_notebook = ttkbootstrap.Notebook(self.window)all_notebook.pack(padx=10, pady=5, fill=ttkbootstrap.BOTH, expand=True)self.frame_one = Frame(all_notebook)self.frame_two = Frame(all_notebook)all_notebook.add(self.frame_one, text='测试界面一')all_notebook.add(self.frame_two, text='测试界面二')def run_main(self):self.main_interface()ui_test_one = UiTestOne(self.frame_one)ui_test_one.show_interface()ui_test_two = UiTestTwo(self.frame_two)ui_test_two.show_interface()self.window.mainloop()if __name__ == '__main__':ui_test = UiTest()ui_test.run_main()

在test_two.py写入界面二,代码如下:

import ttkbootstrap
import tkinter as tkfrom tkinter import ttk, scrolledtext, Frame, VERTICALclass UiTestTwo(object):def __init__(self, window):self.frame_two = windowdef show_interface(self):"""Combobox组件"""global comvalue_operatorcomvalue_operator = tk.StringVar()value_operator = ttk.Combobox(self.frame_two, textvariable=comvalue_operator, width=15)value_operator["values"] = ["1", "2"]value_operator.current(0)value_operator.place(x=550, y=50)"""ScrolledText组件"""scr_explain = scrolledtext.ScrolledText(self.frame_two, font=('微软雅黑', 10), width=102, height=11)scr_explain.place(x=10, y=400)"""Style使用"""style = ttk.Style()style.configure('custom.TButton', font=('宋体', 20), background='red')style.configure("My.TButton", font=('微软雅黑', 10), width=9, height=2)test_button_one = ttkbootstrap.Button(self.frame_two, text="测试按键一")test_button_one["style"] = "custom.TButton"test_button_one.place(x=750, y=50)test_button_two = ttkbootstrap.Button(self.frame_two, text="测试按键二")test_button_two["style"] = "My.TButton"test_button_two.place(x=750, y=150)"""Meter组件"""progress_bar = ttkbootstrap.Meter(master=self.frame_two,bootstyle="success",subtextstyle="warning",stripethickness=10,metertype="full",  # 将仪表显示为一个完整的圆形或半圆形(semi)# wedgesize=5, #设置弧周围的指示器楔形长度,如果大于 0,则此楔形设置为以当前仪表值为中心的指示器amounttotal=100,  # 仪表的最大值,默认100amountused=100,  # 仪表的当前值metersize=130,  # 仪表大小showtext=True,  # 指示是否在仪表上显示左、中、右文本标签interactive=False,  # 是否可以手动调节数字的大小# textleft='进度:',  # 插入到中心文本左侧的短字符串textright='%',textfont="-size 20",  # 中间数字大小# subtext="  ",subtext="完成",subtextfont="-size 8",  # 文本大小)progress_bar.place(x=550, y=150)"""Treeview组件"""tree_top = ["信道", "制式", "速率", "频宽"]frame = Frame(self.frame_two)frame.place(x=10, y=10, width=500)# 设置x轴和y轴的滚动条ybar = ttk.Scrollbar(frame, orient=VERTICAL)ybar.pack(side='right', fill='y')xbar = ttk.Scrollbar(frame, orient=tk.HORIZONTAL)xbar.pack(side='bottom', fill='x')# 创建表格self.tree_date = ttk.Treeview(frame, show='tree headings', height=15)self.tree_date.pack(side='left', fill='y')self.tree_date.configure(yscrollcommand=ybar.set, xscrollcommand=xbar.set)# 使用command参数来绑定treevievwybar.config(command=self.tree_date.yview)xbar.config(command=self.tree_date.xview)# 定义列date_columns = tree_topself.tree_date["columns"] = date_columns# 设置列宽度self.tree_date.column('#0', width=70, anchor="center", stretch=False)self.tree_date.column(tree_top[0], width=120, anchor="center")self.tree_date.column(tree_top[1], width=120, anchor="center")self.tree_date.column(tree_top[2], width=120, anchor="center")self.tree_date.column(tree_top[3], width=120, anchor="center")# 添加列名self.tree_date.heading("#0", text="场景")for j in date_columns:self.tree_date.heading(j, text=j)

本次讲解UI常用组件的写法,后续将更新组件的调用方法,有疑问欢迎找博主进行答疑,我是活动的笑脸,希望大家积极点赞,本篇到此结束。

这篇关于python使用tkinter和ttkbootstrap制作UI界面(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/918526

相关文章

MySQL 日期时间格式化函数 DATE_FORMAT() 的使用示例详解

《MySQL日期时间格式化函数DATE_FORMAT()的使用示例详解》`DATE_FORMAT()`是MySQL中用于格式化日期时间的函数,本文详细介绍了其语法、格式化字符串的含义以及常见日期... 目录一、DATE_FORMAT()语法二、格式化字符串详解三、常见日期时间格式组合四、业务场景五、总结一、

浅析Python中的绝对导入与相对导入

《浅析Python中的绝对导入与相对导入》这篇文章主要为大家详细介绍了Python中的绝对导入与相对导入的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1 Imports快速介绍2 import语句的语法2.1 基本使用2.2 导入声明的样式3 绝对import和相对i

Python中配置文件的全面解析与使用

《Python中配置文件的全面解析与使用》在Python开发中,配置文件扮演着举足轻重的角色,它们允许开发者在不修改代码的情况下调整应用程序的行为,下面我们就来看看常见Python配置文件格式的使用吧... 目录一、INI配置文件二、YAML配置文件三、jsON配置文件四、TOML配置文件五、XML配置文件

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C

MySQL InnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据

《MySQLInnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据》mysql的ibdata文件被误删、被恶意修改,没有从库和备份数据的情况下的数据恢复,不能保证数据库所有表数据... 参考:mysql Innodb表空间卸载、迁移、装载的使用方法注意!此方法只适用于innodb_fi

Python中conda虚拟环境创建及使用小结

《Python中conda虚拟环境创建及使用小结》本文主要介绍了Python中conda虚拟环境创建及使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录0.前言1.Miniconda安装2.conda本地基本操作3.创建conda虚拟环境4.激活c

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解

SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)

《SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)》本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数... 目录SpringBoot使用Jasypt对YML文件配置内容进行加密(例:数据库密码加密)前言一、J

Spring Boot 中正确地在异步线程中使用 HttpServletRequest的方法

《SpringBoot中正确地在异步线程中使用HttpServletRequest的方法》文章讨论了在SpringBoot中如何在异步线程中正确使用HttpServletRequest的问题,... 目录前言一、问题的来源:为什么异步线程中无法访问 HttpServletRequest?1. 请求上下文与线

在 Spring Boot 中使用异步线程时的 HttpServletRequest 复用问题记录

《在SpringBoot中使用异步线程时的HttpServletRequest复用问题记录》文章讨论了在SpringBoot中使用异步线程时,由于HttpServletRequest复用导致... 目录一、问题描述:异步线程操作导致请求复用时 Cookie 解析失败1. 场景背景2. 问题根源二、问题详细分