@用Tkinter打造GUI开发工具(23)软件窗口皮肤

用Tkinter打造GUI开发工具(23)软件窗口皮肤

Tkinter 是 Python 的标准 GUI 库,它是一个跨平台的脚本图形界面接口(GUI)程序,且可以快速的创建 GUI 应用程序。Tkinter库是一个跨平台的窗口应用程序,使用它设计的程序可以在Windows、Mac、Linux系统上执行。由于 Tkinter 是内置到 Python 的安装包中,只要安装好 Python 之后就能导入Tkinter 库,而且Python自带编辑器IDLE 也是用 Tkinter 编写而成。Tkinter能够开发各种应用程序窗口界面。一些常见的库,例如Turtle库,EasyGui库都是建立在Tkinter库的基础上。用户可以轻松运用复制、粘贴、修改等方式,快速搭建用户自己的GUI程序界面。
很多朋友认为Tkinter做的软件很丑,这都是误解.因为这些朋友不会Tkinter. 我们这节介绍Tkinter的美化设计.
首先看看我用Tkinter开发的软件界面,这个界面有背景音乐。

相关代码如下,中间弹出窗口的文字都是程序生成的.

g.root = tk.Tk()
pygame.mixer.init()
musicfile='mus/1.mid'  #背景音乐名
pygame.mixer.music.load(musicfile)  #加载背景音乐文件
pygame.mixer.music.set_volume(0.4)  #设置背景音量
pygame.mixer.music.play(loops=2)   #播放一次
g.root.title(g.title)
window1 = tk.IntVar(g.root, value=0)
pil_image =Image.open('img//bj.jpg')
#获取图像的原始大小
w, h = pil_image.size
w2=w+1
h2=h+10
pil_image = resize(w, h, w2 , h2, pil_image)
r2,g2,b2=RGB(g.cns['gold'])
drawFont2(pil_image,10,10,"小白量化框架聚宽专版",size=40,r=r2,g=g2,b=b2)
r2,g2,b2==RGB(g.cns['gold'])
drawFont2(pil_image,80,50,"--大众化基于人工智能的量化投资系统",size=26,r=r2,g=g2,b=b2)
drawFont3(pil_image,10,70,"超越!",size=80,r=255,g=0,b=0)
drawFont3(pil_image,110,140,"是我们的每一步!",size=54,r=255,g=255,b=0)
tk_image = ImageTk.PhotoImage(pil_image)
username = tk.StringVar()
password = tk.StringVar()  def loginCheck():  name = username.get()  secret =password.get()  try:x=auth(name,secret)  #用户登录,name是用户名,secret是用户密码except:x="error"if x!='error' or x=='' :g.login=Truepygame.mixer.music.stop()  #停止背景音乐pygame.mixer.quit() #退出音乐mixer模块g.status.set(5,'登录')  #格式输出信息g.status.text(1,'') #在状态栏2输出信息w1.destroy()else:  g.status.text(2,'账号或密码错误!') #在状态栏2输出信18578息            showinfo(title='错误', message='账号或密码错误!')g.login=Falsedef rtnkey(self,event=None):loginCheck()w1 = myWindow(g.root,g.title,w2,h2)  #创建弹出窗口
showIco(w1.top,g.ico)  #设置窗口图标
w1.overturn() #隐藏窗口边框
label=tk.Label(w1.top,image=tk_image)
label.place(x=0, y=0, relwidth=1, relheight=1,bordermode=tk.OUTSIDE)Label(w1.top, text = '聚宽账户: ',font = 'Helvetica -18 bold',bg=g.cns['gold']).place(x=90, y=210)
Entry(w1.top, textvariable=username,font = 'Helvetica -18 bold').place(x=210, y=210)
username.set(g.user)
Label(w1.top, text = '聚宽密码: ',font = 'Helvetica -18 bold',bg=g.cns['gold']).place(x=90, y=250)
pas=Entry(w1.top, textvariable=password, show='*',font = 'Helvetica -18 bold')
pas.place(x=210, y=250)
pas.bind('<Key-Return>', rtnkey)
Button(w1.top, text='登  陆', command=loginCheck,font = 'Helvetica -18 bold',bg=g.cns['gold'],fg='blue').place(x=90, y=290)
Button(w1.top, text='退  出', command=w1.destroy,font = 'Helvetica -18 bold',bg=g.cns['gold'],fg='blue').place(x=350, y=290)
setCenter(w1.top,tk_image.width()+4,tk_image.height()+4)
reSizable(w1.top,False, False)
robot_init()
MainPagejq(g.root)

完整代码见《零基础搭建量化投资系统――以Python为工具》一书提供的完整示例。
该书在京东、淘宝、当当网上销售,欢迎大家订购正版书。

《零基础搭建量化投资系统――以Python为工具》第8章目录

第8章 视窗程序开发模块Tkinter8.1 Tkinter的使用8.2 小部件的标准属性8.2.1 Tkinter控件的属性8.2.2 Tkinter控件属性的值和单位1. 维度单位2. 坐标系统3. 颜色4. 字体类型5. 对齐方式6. 浮雕风格7. 位图8. 鼠标样式9. 图片8.3 Tkinter主窗口8.4 Toplevel顶层子窗口8.5 创建窗口菜单8.6 创建弹出菜单8.7 控件的几何布局管理方法1. 控件的pack布局2. 控件的grid布局3. 控件的place布局8.8 Tkinter常用控件1. Label控件2. Frame控件3. Button控件4. Entry控件5. 工具条和状态栏6. Text控件7. Scrollbar控件8. Canvas控件8.9 Tkinter的事件1. 事件名称2. 事件类型3. 事件修饰符4. 编写事件处理程序8.10 Tkinter的主题控件Ttk1. ttk.Notebook控件2. ttk.Treeview控件8.11 Tkinter的扩展控件Tix1. 使用Tix2. 表格几何管理器Form3. tix.Balloon气球窗口小部件4. tix.ButtonBox按钮框5. tix.ComboBox组合框控件6. tix.Control控制窗口

我们希望其它风格的窗口软件界面,这就涉及到软件皮肤设计的问题,下面我们给出2个简单的例子,希望抛砖引玉。

这个界面的代码如下。

# -*- coding: utf-8 -*-
import  tkinter.ttk  as  ttk   #导入Tkinter.ttk
from PIL import Image, ImageTk
import  HP_tk  as  htk   #导入htkroot=htk.MainWindow(title='tkinter主窗口',x=0,y=0,w=800, h=577,picture='img/2.jpg',zoom=True,center=True)
root.attributes('-topmost',1)  #参数1,设置顶层窗口,覆盖其它窗口。
root.overrideredirect(True)
png1= ImageTk.PhotoImage(Image.open('img/ska1.png'))
png2= ImageTk.PhotoImage(Image.open('img/ska2.png'))
png3= ImageTk.PhotoImage(Image.open('img/ska3.png'))def Destroy():root.destroy()bt1=ttk.Button(root, width=12, image=png1, command=root.Destroy,cursor='hand2',style='Btn.TButton')
bt2=ttk.Button(root, width=12, image=png2, command=root.setmin,cursor='hand2',style='Btn.TButton')
bt3=ttk.Button(root, width=12, image=png3, command=root.setmax,cursor='hand2',style='Btn.TButton')
bt1.place(x=14,y=544)
bt2.place(x=40,y=544)
bt3.place(x=66,y=544)root.mainloop()

下面再看一个演示。这里子可以放大或缩小用户窗口。

(正常窗口)


(最小化窗口)

(最大化全屏窗口)

# -*- coding: utf-8 -*-
import  tkinter  as  tk   #导入Tkinter
import  tkinter.ttk  as  ttk   #导入Tkinter.ttk
from PIL import Image, ImageTk
import  HP_tk  as  htk   #导入htkclass skin(tk.Frame): # 继承Frame类  def __init__(self, root=None):  self.staut=Trueself.minstaut=Falseself.width=root.winfo_width()   #获取窗口宽度(单位:像素)self.height=root.winfo_height()  #获取窗口高度(单位:像素)tk.Frame.__init__(self, root)  self.root = root #定义内部变量root  self.s = ttk.Style()self.s.configure('.', background='orange', border=100)self.s.configure('App.TFrame', background='yellow')self.s.configure('Btn.TButton', background='light blue', border=10)self.v1=tk.Frame(self,background='light blue')self.v1.pack(side=tk.TOP, fill=tk.X, expand=1,ipady=1,pady=1,ipadx=1,padx=1)self.img0=Image.open('ico/kchart.ico')self.img1=Image.open('ico/katomic.ico')self.img2=Image.open('ico/kpersonalizer.ico')self.img3=Image.open('ico/windowlist.ico')self.img4=Image.open('ico/exit.ico')self.img0=htk.imgresize2(48,48,self.img0)self.img1=htk.imgresize2(48,48,self.img1)self.img2=htk.imgresize2(48,48,self.img2)self.img3=htk.imgresize2(48,48,self.img3)self.img4=htk.imgresize2(48,48,self.img4)self.png0= ImageTk.PhotoImage(self.img0)self.png1= ImageTk.PhotoImage(self.img1)self.png2= ImageTk.PhotoImage(self.img2)self.png3= ImageTk.PhotoImage(self.img3)self.png4= ImageTk.PhotoImage(self.img4)self.t0 = ttk.Button(self.v1, width=10, image=self.png0, command=self.Destroy,cursor='hand2',style='Btn.TButton')self.t1 = ttk.Button(self.v1, width=10, image=self.png1, command=self.Destroy,cursor='hand2',style='Btn.TButton')self.t2 = ttk.Button(self.v1, width=10, image=self.png2, command=self.setmin,cursor='hand2',style='Btn.TButton')self.t3 = ttk.Button(self.v1, width=10, image=self.png3, command=self.setmax,cursor='hand2',style='Btn.TButton')self.t4 = ttk.Button(self.v1, width=10, image=self.png4, command=self.Destroy,cursor='hand2',style='Btn.TButton')self.t0.pack(side=tk.LEFT)self.t4.pack(side=tk.RIGHT)self.t3.pack(side=tk.RIGHT)self.t2.pack(side=tk.RIGHT)self.t1.pack(side=tk.RIGHT)self.pack(side=tk.TOP,fill=tk.X)self.Overturn()def Destroy(self):self.root.destroy()def Overturn(self):self.root.attributes('-topmost',1)  #参数1,设置顶层窗口,覆盖其它窗口。self.root.overrideredirect(True)def setmin(self):if self.minstaut:self.root.geometry('{}x{}'.format(self.width,self.height))  #改变窗口大小self.minstaut=Falseelse:self.root.geometry('{}x{}'.format(150,30))  #改变窗口大小self.minstaut=Truedef setmax(self):if self.staut:self.root.state("zoomed")  #窗口最大化。self.staut=Falseelse:self.root.state("normal")  #设置为普通窗口。self.staut=Trueroot=htk.MainWindow(title='tkinter主窗口',x=0,y=0,w=800, h=600,zoom=False,center=True)
sk=skin(root)
maimframe=tk.Frame(root,bg='#1E488F',bd=2)
maimframe.pack(fill=tk.BOTH, expand=tk.YES)status=htk.StatusBar(maimframe)    #建立状态栏
status.pack(side=tk.BOTTOM, fill=tk.X) #把状态栏放到窗口底部
status.clear()
status.text(0,'状态栏') #在状态栏1输出信息
status.text(1,'超越自我!') #在状态栏2输出信息
status.text(2,'超越!是我们的每一步!')
status.text(3,'版权所有')
status.text(4,'侵权必究')
status.text(5,'设计:小白')
status.config(1,color='red') #改变状态栏2信息颜色
status.config(3,color='green') #改变状态栏2信息颜色
status.config(4,color='blue') #改变状态栏2信息颜色root.mainloop()

对以上代码有兴趣的朋友可以购买《零基础搭建量化投资系统――以Python为工具》书,加入读者群,下载全部资源和代码。

独狼荷蒲qq:2775205
Python Tkinter小白量化群:524949939
电话微信:18578755056
微信公众号:独狼股票分析

用Tkinter打造GUI开发工具(23)软件窗口皮肤相关推荐

  1. [转载] 用Tkinter打造GUI开发工具(45)用Tkinter做自己的中文代码编辑器

    参考链接: Python | 使用Tkinter的简单注册表格 用Tkinter打造GUI开发工具(45)用Tkinter做自己的中文代码编辑器 前面我们给了Tkinter接管Python输入和输出的 ...

  2. 用Tkinter打造GUI开发工具(8)对齐方式和浮雕风格

    用Tkinter打造GUI开发工具(8)对齐方式和浮雕风格 我们这篇继续介绍Tkinter通用属性. 首先介绍对齐方式.Tkinter小部件的对齐方式用属性anchor.anchor取值有tk.N(上 ...

  3. 用Tkinter打造GUI开发工具(14)Tkinter小部件的Label标签部件

    用Tkinter打造GUI开发工具(14)Tkinter小部件的Label标签部件 标签Label部件是Tkinter最常用的部件之一,其语法格式如下. label=tk.Label ( master ...

  4. 用Tkinter打造GUI开发工具(7)字体类型

    Tkinter字体类型根据Python使用的平台,可能会有三种方法来指定类型的风格. 1)作为一个元组的第一个元素是字体,紧随其后的是分大小,可选地紧随其后 字符串包含一个或多个风格修饰符粗体.斜体. ...

  5. python编程工具p-python Gui开发工具选择

    Python最大的特点就在于她的快速开发功能.作为一种胶水型语言,python几乎可以渗透在我们编程过程中的各个领域.这里我简单介绍一下用python进行gui开发的一些选择. 1.Tkinter T ...

  6. python自带gui_Python GUI开发工具中五种类型的相关介绍

    Python这一计算机语言不仅具有十分简捷的操作功能,它还具有十分快速的相关开发功能,此文主要向你介绍的是关于Python GUI开发工具在实际相关应用操作过程中的具体操作,以及这几种开发工具相关内容 ...

  7. xilinx fpga 开发工具vivado 软件的安装,使用详细教程

    文章目录 xilinx fpga 开发工具vivado 软件的安装,使用详细教程 xilinx fpga 开发工具vivado 软件的安装,使用详细教程 点此下载vivado软件的安装说明文档 点此下 ...

  8. (原创)LEON3入门教程(一):什么是LEON3?需要哪些开发工具和软件?

    写在最前面的话 2010年年底我开始接触LEON3,花了3个月时间把LEON3源代码大致阅读完,然后参照Gaisler Research 网站上提供的leon3-altera-ep3c25源代码,修改 ...

  9. Python GUI 开发工具

    Python Tkinter教程(GUI图形界面开发教程):http://c.biancheng.net/python/tkinter/ PyQt5教程:http://code.py40.com/fa ...

最新文章

  1. 拼命学的编程,你却可能错过一个亿!
  2. 【算法】一个简单的ISODATA原理
  3. topic数量是指什么_一个网站的IP、UV和PV到底是什么
  4. 转!最适合新手小白的8个python开发环境(内附python IDE最新下载地址+软件激活码+长期有效)
  5. 动态调用object php,PHP动态调用,大家都来谈吧
  6. 蛮力法 —— 求解迷宫问题 —— DFS和BFS
  7. springboot之redis整合
  8. VB6 mysql二进制读取,vb6关于VB以二进制(binary)方式读取文本内容
  9. Codeforces 988D Points and Powers of Two ( 思维 || 二的幂特点 )
  10. 敏捷开发一千零一问:怎样处理重要但不明白的任务?
  11. Xcode 打包 framework
  12. 华氏摄氏转换 java_Java题目实现将华氏温度转换成摄氏温度
  13. 二线城市疯狂抢人,技术人才何去何从?
  14. Arduino框架下ESP8266获取和风天气的第三方库实现天气时钟制作思路
  15. 【给量化行情插上翅膀】天翼云电脑上实践纯Python通过LMDB加速股票行情读写速度
  16. Linux中彻底删除文件
  17. AutoCAD套合(叠加)卫星影像和矢量路网数据-CAD配准
  18. c语言余弦函数,C语言编写余弦函数曲线
  19. unity拖拽UI生成Cube并跟随鼠标移动,放置点可放下
  20. Elasticsearch实战 | match_phrase搜不出来,怎么办?

热门文章

  1. php强行横屏,android强制横屏apk下载
  2. SAY GOODBYE TO SPURS再见,马刺!
  3. MATLAB 图像特征点检测(6个方法)
  4. CSV纯文本数据格式
  5. 如何把爱情公寓5引入到我的html
  6. html将表格中的单元格合并单元格,包含合并单元格的HTML表格中的单元格宽度
  7. Vista破解方法深度总结(目前共7种方法) ayconanw撰写
  8. 自媒体全新赛道:三农领域怎么做?
  9. LKT6850安全MCU(二):系统架构
  10. gsonformat安装怎么使用_Intellij IDEA 插件 GsonFormat 快速创建JSON格式JavaBean