IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> Python-tkinter界面美化增强计划(一):ExButton -> 正文阅读

[Python知识库]Python-tkinter界面美化增强计划(一):ExButton

用了tkinter很长时间了,一直都是觉得tkinter功能挺到位,但就是不够美观,所以就有了这个窗口美化计划(我管它叫Extkinter),这篇文章介绍一下它的第一个组件:按钮(ExButton)

运行效果

先上图,看看使用ExButton做出来界面的实际运行效果

这里只是一个使用示例,按钮可以点,但没有实际作用(本来想录制一个gif动图的但ps一直抽风)

使用方法

先放出上面运行效果界面的代码

from Extkinter import *
import tkinter.ttk as ttk


class Main(Tk):
    def __init__(self):
        super().__init__()
        self.top_frame = None
        self.button_frame_1 = None
        self.button_frame_2 = None
        self.main_frame_1 = None
        self.main_frame_2 = None
        self.top_button_arr = []
        self.left_button_arr = []
        self.main_button_arr = []
        self.initialize()
        self.frame_initialize()
        self.interface_initialize()

    def initialize(self):
        self.title("数据管理系统")
        self.geometry("1000x620+%d+%d" % (self.winfo_screenwidth() / 2 - 500,
                                          self.winfo_screenheight() / 2 - 390))
        self.resizable(False, False)

    def frame_initialize(self):
        self.top_frame = Canvas(self, bg="black", height=60, width=1000, highlightthickness=0)
        self.top_frame.place(x=0, y=0)
        self.button_frame_1 = Canvas(self, bg="White", height=255, width=180, highlightthickness=0)
        self.button_frame_1.place(x=30, y=90)
        self.button_frame_2 = Canvas(self, bg="White", height=215, width=180, highlightthickness=0)
        self.button_frame_2.place(x=30, y=375)
        self.main_frame_1 = Canvas(self, bg="White", height=55, width=730, highlightthickness=0)
        self.main_frame_1.place(x=240, y=90)
        self.main_frame_2 = Canvas(self, bg="White", height=440, width=730, highlightthickness=0)
        self.main_frame_2.place(x=240, y=150)

    def interface_initialize(self):
        # 布置顶层界面
        Label(self.top_frame, text="数据管理系统", bg="black", fg="white", font=("华文细黑", 18)).place(x=30, y=13)
        top_button_1 = ExButton(self.top_frame, height=60, width=130, text="首页", font=("华文细黑", 15),
                                style="vertical_color", command=self.pass_command)
        top_button_1.set(button_list=self.top_button_arr, font_color=("White", "White"), color=("Black", "Black"),
                         active_color=("DeepSkyBlue", "Black"))
        top_button_1.place(x=300, y=0)
        top_button_2 = ExButton(self.top_frame, height=60, width=130, text="产品购买", font=("华文细黑", 15),
                                style="vertical_color", command=self.pass_command)
        top_button_2.set(button_list=self.top_button_arr, font_color=("White", "White"), color=("Black", "Black"),
                         active_color=("DeepSkyBlue", "Black"))
        top_button_2.place(x=430, y=0)
        top_button_3 = ExButton(self.top_frame, height=60, width=130, text="关于我们", font=("华文细黑", 15),
                                style="vertical_color", command=self.pass_command)
        top_button_3.set(button_list=self.top_button_arr, font_color=("White", "White"), color=("Black", "Black"),
                         active_color=("DeepSkyBlue", "Black"))
        top_button_3.place(x=560, y=0)
        top_button_4 = ExButton(self.top_frame, height=60, width=120, text="登录", font=("华文细黑", 12),
                                command=self.pass_command)
        top_button_4.set(font_color=("White", "White"), color=("Black", "Black"))
        top_button_4.place(x=880, y=0)

        # 布置按钮界面
        Label(self.button_frame_1, text="数据库", bg="white", font=("微软雅黑", 16)).place(x=30, y=10)
        left_button = ExButton(self.button_frame_1, height=40, width=180, text="数据中心", font=("微软雅黑", 11),
                               command=self.pass_command)
        left_button.set(button_list=self.left_button_arr, color=("White", "White"),
                        active_color=("DeepSkyBlue", "#F0F0F0"))
        left_button.place(x=0, y=55)
        left_button = ExButton(self.button_frame_1, height=40, width=180, text="创建数据", font=("微软雅黑", 11),
                               command=self.pass_command)
        left_button.set(button_list=self.left_button_arr, color=("White", "White"),
                        active_color=("DeepSkyBlue", "#F0F0F0"))
        left_button.place(x=0, y=95)
        left_button = ExButton(self.button_frame_1, height=40, width=180, text="导入数据", font=("微软雅黑", 11),
                               command=self.pass_command)
        left_button.set(button_list=self.left_button_arr, color=("White", "White"),
                        active_color=("DeepSkyBlue", "#F0F0F0"))
        left_button.place(x=0, y=135)
        left_button = ExButton(self.button_frame_1, height=40, width=180, text="导出数据", font=("微软雅黑", 11),
                               command=self.pass_command)
        left_button.set(button_list=self.left_button_arr, color=("White", "White"),
                        active_color=("DeepSkyBlue", "#F0F0F0"))
        left_button.place(x=0, y=175)
        left_button = ExButton(self.button_frame_1, height=40, width=180, text="设置", font=("微软雅黑", 11),
                               command=self.pass_command)
        left_button.set(button_list=self.left_button_arr, color=("White", "White"),
                        active_color=("DeepSkyBlue", "#F0F0F0"))
        left_button.place(x=0, y=215)

        # 布置第二层按钮界面
        Label(self.button_frame_2, text="个人中心", bg="white", font=("微软雅黑", 16)).place(x=30, y=10)
        left_button = ExButton(self.button_frame_2, height=40, width=180, text="账号管理", font=("微软雅黑", 11),
                               command=self.pass_command)
        left_button.set(button_list=self.left_button_arr, color=("White", "White"),
                        active_color=("DeepSkyBlue", "#F0F0F0"))
        left_button.place(x=0, y=55)
        left_button = ExButton(self.button_frame_2, height=40, width=180, text="我的收藏", font=("微软雅黑", 11),
                               command=self.pass_command)
        left_button.set(button_list=self.left_button_arr, color=("White", "White"),
                        active_color=("DeepSkyBlue", "#F0F0F0"))
        left_button.place(x=0, y=95)
        left_button = ExButton(self.button_frame_2, height=40, width=180, text="我的数据", font=("微软雅黑", 11),
                               command=self.pass_command)
        left_button.set(button_list=self.left_button_arr, color=("White", "White"),
                        active_color=("DeepSkyBlue", "#F0F0F0"))
        left_button.place(x=0, y=135)
        left_button = ExButton(self.button_frame_2, height=40, width=180, text="购买记录", font=("微软雅黑", 11),
                               command=self.pass_command)
        left_button.set(button_list=self.left_button_arr, color=("White", "White"),
                        active_color=("DeepSkyBlue", "#F0F0F0"))
        left_button.place(x=0, y=175)

        # 布置页眉
        Label(self.main_frame_1, text="数据中心", bg="White", font=("幼圆", 15)).place(x=30, y=13)
        seek_button = ExButton(self.main_frame_1, text="搜索", height=20, width=50, command=self.pass_command,
                               font=("幼圆", 12))
        seek_button.place(x=650, y=18)
        seek_entry = ttk.Entry(self.main_frame_1)
        seek_entry.place(x=480, y=18)

        # 布置主界面内容
        main_button = ExButton(self.main_frame_2, text="推荐", height=35, width=70, command=self.pass_command,
                               font=("华文细黑", 11), style="vertical_color")
        main_button.set(button_list=self.main_button_arr, active_color=("#F0F0F0", "#F0F0F0"))
        main_button.place(x=0, y=0)
        main_button = ExButton(self.main_frame_2, text="热点", height=35, width=70, command=self.pass_command,
                               font=("华文细黑", 11), style="vertical_color")
        main_button.set(button_list=self.main_button_arr, active_color=("#F0F0F0", "#F0F0F0"))
        main_button.place(x=70, y=0)
        main_button = ExButton(self.main_frame_2, text="社区", height=35, width=70, command=self.pass_command,
                               font=("华文细黑", 11), style="vertical_color")
        main_button.set(button_list=self.main_button_arr, active_color=("#F0F0F0", "#F0F0F0"))
        main_button.place(x=140, y=0)
        Label(self.main_frame_2, text="Python数据爬取\t\t2021-8-8", bg="White", font=("华文细黑", 13)).place(x=25, y=50)
        Label(self.main_frame_2, text="班级成绩数据分析\t\t2021-8-9", bg="White", font=("华文细黑", 13)).place(x=25, y=85)
        Label(self.main_frame_2, text="股票走势数据\t\t2021-8-10", bg="White", font=("华文细黑", 13)).place(x=25, y=120)
        Label(self.main_frame_2, text="硬件价格走势\t\t2021-8-10", bg="White", font=("华文细黑", 13)).place(x=25, y=155)

    def pass_command(self):
        pass


if __name__ == "__main__":
    run = Main()
    run.mainloop()

基础功能介绍:

def __init__(self, master, command, height=0, width=0, text="", font="宋体", style="horizontal_color")

ExButton的析构函数有以下属性:
1.master为按钮所放置的父物体
2.command为执行的函数(正常的tkinter按钮不一定要绑定函数,但这里必须要,目前还没找到解决方法)
3.height,width为按钮的长度、宽度(像素)
4.text为按钮的文本
5.font为按钮的字体与大小,传入参数为(“字体名字”, 字体大小),和tkinter组件一样
6.style为按钮的样式,如果为"horizontal_color",那么按钮为左右颜色样式(参考运行图片中左边的那一排按钮),如果为"vertical_color",那么按钮为上下颜色样式(参考运行图片中上面的那一排按钮)

set函数介绍:

def set(self, button_list=None, font_color=("#000000", "#000000"), color=("#FFFFFF", "#FFFFFF"),
            active_color=("#FFD1FC", "#F0F0F0"))

在创建完ExButton后,可以使用set函数对其进一步地修改
set参数介绍:
1.button_list
button_list为按钮组功能,需要预先创建一个空列表,再将其传入ExButton set函数中的button_list,这样一来ExButton就被装进了那个列表,一个列表里所有的ExButton只能有一个是被选中状态。根据运行图片,左边的那一排按钮都被装进了left_button_arr,故只会有一个按钮是亮的,上面那一排黑蓝色的按钮被装进了top_button_arr,故上面也只亮一个按钮,两边不会互相影响。如果按钮没被装进任何列表,那么它被点击就不会有高亮显示,但依然能执行函数。(ps.默认界面创建出来时会先高亮列表第一个ExButton)
2.font_color
传入font_color里的两个字符串分别对应按钮未点击、被点击时的字体颜色
3.color与active_color
若style为"horizontal_color",那么传入color和active_color里的两个字符串分别对应按钮未被按下时和按下时的左、右两个颜色。若style为"vertical_color"那么传入color和active_color里的两个字符串分别对应按钮未被按下时和按下时的上、下两个颜色。

ExButton继承了tkinter Canvas,所以ExButton也能使用pack,place,grid的布局方式。

源代码

这里放一下ExButton的源代码,目前只是初代版本,以后会慢慢更新和优化,如果使用过程中有什么bug或不好用的地方欢迎私信,期待大家的支持!!

"""
version:1.1
作者:椎名mikan
"""
from tkinter import *


class ExButton(Canvas):
    def __init__(self, master, command, height=0, width=0, text="", font="宋体", style="horizontal_color"):
        super().__init__(master, height=height, width=width, highlightthickness=0, cursor="hand2")
        self.height = height
        self.width = width
        self.text = text
        self.font = font
        self.command = command
        self.style = style
        self.click_effect = False
        self.button_list = None
        self.color_1 = None
        self.color_2 = None
        self.active_color_1 = None
        self.active_color_2 = None
        self.font_color = None
        self.active_font_color = None
        self.bind("<Button-1>", self.callback)
        self.set()

    def set(self, button_list=None, font_color=("#000000", "#000000"), color=("#FFFFFF", "#FFFFFF"),
            active_color=("#FFD1FC", "#F0F0F0")):
        self.font_color = font_color[0]
        self.active_font_color = font_color[1]
        self.button_list = button_list
        self.color_1 = color[0]
        self.color_2 = color[1]
        self.active_color_1 = active_color[0]
        self.active_color_2 = active_color[1]
        if self.button_list is not None:
            self.button_list.append(self)
            self.button_list[0].click_effect = True
        self.paint()

    def callback(self, event):
        if event:
            pass
        self.command()
        if self.button_list is not None:
            self.set_active()
            self.paint()

    def set_active(self):
        for i in range(0, len(self.button_list)):
            self.button_list[i].click_effect = False
        self.click_effect = True

    def paint(self):
        if self.button_list is not None:
            for i in range(0, len(self.button_list)):
                if self.button_list[i].style == "horizontal_color":
                    if self.button_list[i].click_effect is True:
                        self.button_list[i].create_rectangle(0, 0, 7, self.button_list[i].height,
                                                             fill=self.button_list[i].active_color_1, outline="")
                        self.button_list[i].create_rectangle(7, 0, self.button_list[i].width,
                                                             self.button_list[i].height,
                                                             fill=self.button_list[i].active_color_2, outline="")
                        self.button_list[i].create_text(self.button_list[i].width / 2, self.button_list[i].height / 2,
                                                        text=self.button_list[i].text, font=self.button_list[i].font,
                                                        fill=self.button_list[i].active_font_color)
                    else:
                        self.button_list[i].create_rectangle(0, 0, 7, self.button_list[i].height,
                                                             fill=self.button_list[i].color_1, outline="")
                        self.button_list[i].create_rectangle(7, 0, self.button_list[i].width,
                                                             self.button_list[i].height,
                                                             fill=self.button_list[i].color_2, outline="")
                        self.button_list[i].create_text(self.button_list[i].width / 2, self.button_list[i].height / 2,
                                                        text=self.button_list[i].text, font=self.button_list[i].font,
                                                        fill=self.button_list[i].font_color)
                elif self.button_list[i].style == "vertical_color":
                    if self.button_list[i].click_effect is True:
                        self.button_list[i].create_rectangle(0, 0, self.button_list[i].width, 5,
                                                             fill=self.button_list[i].active_color_1, outline="")
                        self.button_list[i].create_rectangle(0, 5, self.button_list[i].width,
                                                             self.button_list[i].height,
                                                             fill=self.button_list[i].active_color_2, outline="")
                        self.button_list[i].create_text(self.button_list[i].width / 2, self.button_list[i].height / 2,
                                                        text=self.button_list[i].text, font=self.button_list[i].font,
                                                        fill=self.button_list[i].active_font_color)
                    else:
                        self.button_list[i].create_rectangle(0, 0, self.button_list[i].width, 5,
                                                             fill=self.button_list[i].color_1, outline="")
                        self.button_list[i].create_rectangle(0, 5, self.button_list[i].width,
                                                             self.button_list[i].height,
                                                             fill=self.button_list[i].color_2, outline="")
                        self.button_list[i].create_text(self.button_list[i].width / 2, self.button_list[i].height / 2,
                                                        text=self.button_list[i].text, font=self.button_list[i].font,
                                                        fill=self.button_list[i].font_color)
        else:
            self.create_rectangle(0, 0, 8, self.height, fill=self.color_1, outline="")
            self.create_rectangle(8, 0, self.width, self.height, fill=self.color_2, outline="")
            self.create_text(self.width / 2, self.height / 2, text=self.text, font=self.font, fill=self.font_color)

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-08-11 12:18:43  更:2021-08-11 12:20:12 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 13:47:31-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码