ttkbootstrap界面美化系列之主窗口(二)

2024-03-18 16:52

本文主要是介绍ttkbootstrap界面美化系列之主窗口(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一:创建主窗口

在利用ttkbootstrap构建应用程序时,可以用tkinter传统的tk方法来创建主界面,也可以用ttkbootstrap中的window类来创建,下面我们来看看两者的区别

1,传统方法创建主界面

import tkinter as tk
import ttkbootstrap as ttk
from ttkbootstrap.constants import *root = tk.Tk()
root.title('主窗口')
root.geometry('300x200')b1 = ttk.Button(root, text="Button 1", bootstyle=SUCCESS)
b1.pack(side=LEFT, padx=5, pady=10)b2 = ttk.Button(root, text="Button 2", bootstyle=(INFO, OUTLINE))
b2.pack(side=LEFT, padx=5, pady=10)root.mainloop()

2,新方法创建主界面

import ttkbootstrap as ttk
from ttkbootstrap.constants import *root = ttk.Window( title="主窗口",            #设置窗口的标题size=(300,200),        #窗口的大小)b1 = ttk.Button(root, text="Button 1", bootstyle=SUCCESS)
b1.pack(side=LEFT, padx=5, pady=10)b2 = ttk.Button(root, text="Button 2", bootstyle=(INFO, OUTLINE))
b2.pack(side=LEFT, padx=5, pady=10)root.mainloop()

        从上面的运行界面上看,差异可能看起来很小,但正如您将看到的,Window类使用参数来设置许多属性和质量,而这些属性和质量只能在使用Tk类时使用方法进行设置。此外,Style对象会自动附加到Window对象。

二:window类的属性

可以用以下方法来获取window类的所有属性

print(dir(ttk.Window))

['_Misc__winfo_getint', '_Misc__winfo_parseitem', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_bind', '_configure', '_displayof', '_getboolean', '_getconfigure', '_getconfigure1', '_getdoubles', '_getints', '_grid_configure', '_gridconvvalue', '_last_child_ids', '_loadtk', '_nametowidget', '_noarg_', '_options', '_register', '_report_exception', '_root', '_subst_format', '_subst_format_str', '_substitute', '_tclCommands', '_unbind', '_w', '_windowingsystem', 'after', 'after_cancel', 'after_idle', 'anchor', 'aspect', 'attributes', 'bbox', 'bell', 'bind', 'bind_all', 'bind_class', 'bindtags', 'cget', 'client', 'clipboard_append', 'clipboard_clear', 'clipboard_get', 'colormapwindows', 'columnconfigure', 'command', 'config', 'configure', 'deiconify', 'deletecommand', 'destroy', 'event_add', 'event_delete', 'event_generate', 'event_info', 'focus', 'focus_displayof', 'focus_force', 'focus_get', 'focus_lastfor', 'focus_set', 'focusmodel', 'forget', 'frame', 'geometry', 'getboolean', 'getdouble', 'getint', 'getvar', 'grab_current', 'grab_release', 'grab_set', 'grab_set_global', 'grab_status', 'grid', 'grid_anchor', 'grid_bbox', 'grid_columnconfigure', 'grid_location', 'grid_propagate', 'grid_rowconfigure', 'grid_size', 'grid_slaves', 'group', 'iconbitmap', 'iconify', 'iconmask', 'iconname', 'iconphoto', 'iconposition', 'iconwindow', 'image_names', 'image_types', 'info_patchlevel', 'keys', 'lift', 'loadtk', 'lower', 'mainloop', 'manage', 'maxsize', 'minsize', 'nametowidget', 'option_add', 'option_clear', 'option_get', 'option_readfile', 'overrideredirect', 'pack_propagate', 'pack_slaves', 'place_slaves', 'place_window_center', 'position_center', 'positionfrom', 'propagate', 'protocol', 'quit', 'readprofile', 'register', 'report_callback_exception', 'resizable', 'rowconfigure', 'selection_clear', 'selection_get', 'selection_handle', 'selection_own', 'selection_own_get', 'send', 'setvar', 'size', 'sizefrom', 'slaves', 'state', 'style', 'title', 'tk_bisque', 'tk_focusFollowsMouse', 'tk_focusNext', 'tk_focusPrev', 'tk_setPalette', 'tk_strictMotif', 'tkraise', 'transient', 'unbind', 'unbind_all', 'unbind_class', 'update', 'update_idletasks', 'wait_variable', 'wait_visibility', 'wait_window', 'waitvar', 'winfo_atom', 'winfo_atomname', 'winfo_cells', 'winfo_children', 'winfo_class', 'winfo_colormapfull', 'winfo_containing', 'winfo_depth', 'winfo_exists', 'winfo_fpixels', 'winfo_geometry', 'winfo_height', 'winfo_id', 'winfo_interps', 'winfo_ismapped', 'winfo_manager', 'winfo_name', 'winfo_parent', 'winfo_pathname', 'winfo_pixels', 'winfo_pointerx', 'winfo_pointerxy', 'winfo_pointery', 'winfo_reqheight', 'winfo_reqwidth', 'winfo_rgb', 'winfo_rootx', 'winfo_rooty', 'winfo_screen', 'winfo_screencells', 'winfo_screendepth', 'winfo_screenheight', 'winfo_screenmmheight', 'winfo_screenmmwidth', 'winfo_screenvisual', 'winfo_screenwidth', 'winfo_server', 'winfo_toplevel', 'winfo_viewable', 'winfo_visual', 'winfo_visualid', 'winfo_visualsavailable', 'winfo_vrootheight', 'winfo_vrootwidth', 'winfo_vrootx', 'winfo_vrooty', 'winfo_width', 'winfo_x', 'winfo_y', 'withdraw', 'wm_aspect', 'wm_attributes', 'wm_client', 'wm_colormapwindows', 'wm_command', 'wm_deiconify', 'wm_focusmodel', 'wm_forget', 'wm_frame', 'wm_geometry', 'wm_grid', 'wm_group', 'wm_iconbitmap', 'wm_iconify', 'wm_iconmask', 'wm_iconname', 'wm_iconphoto', 'wm_iconposition', 'wm_iconwindow', 'wm_manage', 'wm_maxsize', 'wm_minsize', 'wm_overrideredirect', 'wm_positionfrom', 'wm_protocol', 'wm_resizable', 'wm_sizefrom', 'wm_state', 'wm_title', 'wm_transient', 'wm_withdraw']

 具体方法怎么使用可以用help来查看

print(help(ttk.Window))
class Window(tkinter.Tk)|  Window(title='ttkbootstrap', themename='litera', iconphoto='', size=None, position=None, minsize=None, maxsize=None, resizable=None, hdpi=True, scaling=None, transient=None, overrideredirect=False, alpha=1.0)||  A class that wraps the tkinter.Tk class in order to provide a|  more convenient api with additional bells and whistles. For more|  information on how to use the inherited `Tk` methods, see the|  [tcl/tk documentation](https://tcl.tk/man/tcl8.6/TkCmd/wm.htm)|  and the [Python documentation](https://docs.python.org/3/library/tkinter.html#tkinter.Tk).||  ![](../../assets/window/window-toplevel.png)||  Examples:||      ```python|      app = Window(title="My Application", themename="superhero")|      app.mainloop()|      ```||  Method resolution order:|      Window|      tkinter.Tk|      tkinter.Misc|      tkinter.Wm|      builtins.object||  Methods defined here:||  __init__(self, title='ttkbootstrap', themename='litera', iconphoto='', size=None, position=None, minsize=None, maxsize=None, resizable=None, hdpi=True, scaling=None, transient=None, overrideredirect=False, alpha=1.0)|      Parameters:||          title (str):|              The title that appears on the application titlebar.||          themename (str):|              The name of the ttkbootstrap theme to apply to the|              application.||          iconphoto (str):|              A path to the image used for the titlebar icon.|              Internally this is passed to the `Tk.iconphoto` method|              and the image will be the default icon for all windows.|              A ttkbootstrap image is used by default. To disable|              this default behavior, set the value to `None` and use|              the `Tk.iconphoto` or `Tk.iconbitmap` methods directly.||          size (Tuple[int, int]):|              The width and height of the application window.|              Internally, this argument is passed to the|              `Window.geometry` method.||          position (Tuple[int, int]):|              The horizontal and vertical position of the window on|              the screen relative to the top-left coordinate.|              Internally this is passed to the `Window.geometry`|              method.||          minsize (Tuple[int, int]):|              Specifies the minimum permissible dimensions for the|              window. Internally, this argument is passed to the|              `Window.minsize` method.||          maxsize (Tuple[int, int]):|              Specifies the maximum permissible dimensions for the|              window. Internally, this argument is passed to the|              `Window.maxsize` method.||          resizable (Tuple[bool, bool]):|              Specifies whether the user may interactively resize the|              toplevel window. Must pass in two arguments that specify|              this flag for _horizontal_ and _vertical_ dimensions.|              This can be adjusted after the window is created by using|              the `Window.resizable` method.||          hdpi (bool):|              Enable high-dpi support for Windows OS. This option is|              enabled by default.||          scaling (float):|              Sets the current scaling factor used by Tk to convert|              between physical units (for example, points, inches, or|              millimeters) and pixels. The number argument is a|              floating point number that specifies the number of pixels|              per point on window's display.||          transient (Union[Tk, Widget]):|              Instructs the window manager that this widget is|              transient with regard to the widget master. Internally|              this is passed to the `Window.transient` method.||          overrideredirect (bool):|              Instructs the window manager to ignore this widget if|              True. Internally, this argument is passed to the|              `Window.overrideredirect(1)` method.||          alpha (float):|              On Windows, specifies the alpha transparency level of the|              toplevel. Where not supported, alpha remains at 1.0. Internally,|              this is processed as `Toplevel.attributes('-alpha', alpha)`.||  place_window_center(self)|      Position the toplevel in the center of the screen. Does not|      account for titlebar height.||  position_center = place_window_center(self)

下面给出常用的参数配置示例:

root = ttk.Window(  title="主窗口",        #设置窗口的标题themename="yeti",     #设置主题yetisize=(300,200),        #窗口的大小position=(200,100),     #窗口所在的位置minsize=(0,0),          #窗口的最小宽高maxsize=(1920,1080),    #窗口的最大宽高resizable=None,         #设置窗口是否可以更改大小alpha=1.0,              #设置窗口的透明度(0.0完全透明))root.place_window_center()    #让显现出的窗口居中
root.resizable(False,False)   #让窗口不可更改大小
root.wm_attributes('-topmost', 1)#让窗口位置其它窗口之上
root.iconbitmap(get_path("ico/zrt.ico"))

官方文档:Window - ttkbootstrap 

三:界面主题

在上面的示例中可以通过themename参数来设置主题,在ttkbootstrap中有很多已经设置好的主题供开发者使用,但是具体有哪些主题呢,可以通过下面的方法来获取

from ttkbootstrap.style import Style# instantiate the style with default theme
style = Style()# instantiate the style with another theme
style = Style(theme='superhero')# check all available themes
for theme in style.theme_names():print(theme)

以下是内置支持的主题: 

cosmo
flatly
litera
minty
lumen
sandstone
yeti
pulse
united
morph
journal
darkly
superhero
solar
cyborg
vapor
simplex
cerculean

官方文档给出了每种主题的样式,大家可以根据自己的审美选择合适的主题

Themes — ttkbootstrap documentation

主题 - ttkbootstrap

下面给出几张官方样例:

 

 

 四:小组件主题

除了上面对整体界面进行主题设置,也可以单独对某个小部件的主题进行设置,如设置按钮的颜色

import ttkbootstrap as ttk
from ttkbootstrap.constants import *root = ttk.Window()for color in root.style.colors:print(color)b = ttk.Button(root, text=color, bootstyle=color)b.pack(side=LEFT, padx=5, pady=5)root.mainloop()

以下是支持内置颜色:

primary
secondary
success
info
warning
danger
light
dark

这篇关于ttkbootstrap界面美化系列之主窗口(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

JavaWeb系列二十: jQuery的DOM操作 下

jQuery的DOM操作 CSS-DOM操作多选框案例页面加载完毕触发方法作业布置jQuery获取选中复选框的值jQuery控制checkbox被选中jQuery控制(全选/全不选/反选)jQuery动态添加删除用户 CSS-DOM操作 获取和设置元素的样式属性: css()获取和设置元素透明度: opacity属性获取和设置元素高度, 宽度: height(), widt

C语言入门系列:探秘二级指针与多级指针的奇妙世界

文章目录 一,指针的回忆杀1,指针的概念2,指针的声明和赋值3,指针的使用3.1 直接给指针变量赋值3.2 通过*运算符读写指针指向的内存3.2.1 读3.2.2 写 二,二级指针详解1,定义2,示例说明3,二级指针与一级指针、普通变量的关系3.1,与一级指针的关系3.2,与普通变量的关系,示例说明 4,二级指针的常见用途5,二级指针扩展到多级指针 小结 C语言的学习之旅中,二级

JavaWeb系列六: 动态WEB开发核心(Servlet) 上

韩老师学生 官网文档为什么会出现Servlet什么是ServletServlet在JavaWeb项目位置Servlet基本使用Servlet开发方式说明快速入门- 手动开发 servlet浏览器请求Servlet UML分析Servlet生命周期GET和POST请求分发处理通过继承HttpServlet开发ServletIDEA配置ServletServlet注意事项和细节 Servlet注

【Java算法】滑动窗口 下

​ ​    🔥个人主页: 中草药 🔥专栏:【算法工作坊】算法实战揭秘 🦌一.水果成篮 题目链接:904.水果成篮 ​ 算法原理 算法原理是使用“滑动窗口”(Sliding Window)策略,结合哈希表(Map)来高效地统计窗口内不同水果的种类数量。以下是详细分析: 初始化:创建一个空的哈希表 map 用来存储每种水果的数量,初始化左右指针 left

【Qt6.3 基础教程 17】 Qt布局管理详解:创建直观和响应式UI界面

文章目录 前言布局管理的基础为什么需要布局管理器? 盒布局:水平和垂直排列小部件示例:创建水平盒布局 栅格布局:在网格中对齐小部件示例:创建栅格布局 表单布局:为表单创建标签和字段示例:创建表单布局 调整空间和伸缩性示例:增加弹性空间 总结 前言 当您开始使用Qt设计用户界面(UI)时,理解布局管理是至关重要的。布局管理不仅关系到UI的外观,更直接影响用户交互的体验。本篇博

C语言入门系列:初识函数

文章目录 一,C语言函数与数学函数的区别1,回忆杀-初中数学2,C语言中的函数 二, 函数的声明1,函数头1.1,函数名称1.2,返回值类型1.3,参数列表 2,函数体2.1,函数体2.2,return语句 三,main函数四,函数的参数与传递方式1,实参和形参1.1,函数定义(含形参)1.2,函数调用(使用实参) 2,参数传递方式2.1,值传递2.2,引用传递 五,函数原型与预声明1,

django学习入门系列之第三点《案例 小米商城头标》

文章目录 阴影案例 小米商城头标往期回顾 阴影 设置阴影 box-shadow:水平方向 垂直方向 模糊距离 颜色 box-shadow: 5px 5px 5px #aaa; 案例 小米商城头标 目标样式: CSS中的代码 /*使外边距等于0,即让边框与界面贴合*/body{margin: 0;}/*控制父级边框*/.header{backgroun

MyBatis系列之分页插件及问题

概述 无论是C端产品页面,还是后台系统页面,不可能一次性将全部数据加载出来。后台系统一般都是PC端登录,用Table组件(如Ant Design Table)渲染展示数据,可点击列表的下一页(或指定某一页)查看数据。C端产品如App,在下滑时可查看更多数据,看起来像是一次性加载数据,实际上也是分批请求后台系统获取数据。而这,就是分页功能。 如果没有使用Hibernate或MyBatis这样的O

物联网系统运维——移动电商应用发布,Tomcat应用服务器,实验CentOS 7安装JDK与Tomcat,配置Tomcat Web管理界面

一.Tomcat应用服务器 1.Tomcat介绍 Tomcat是- -个免费的开源的Ser Ivet容器,它是Apache基金会的Jakarta 项目中的一个核心项目,由Apache, Sun和其他一 些公司及个人共同开发而成。Tomcat是一一个小型的轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。 在Tomcat中,应用程序的成部署很简

CSS列表属性:list-style系列属性详解

CSS(层叠样式表)是用于控制网页样式的一种语言,它允许开发者以一种非常灵活的方式来设置网页元素的外观。在CSS中,list-style属性族是专门用来设置列表样式的。列表是网页设计中常见的元素,它们可以是有序列表(<ol>)或无序列表(<ul>)。list-style系列属性允许你自定义列表项前的标记,包括类型、位置和图像。 1. list-style-type list-style-typ