<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tkinter &#8211; 李辉 / Grey Li</title>
	<atom:link href="https://greyli.com/tag/tkinter/feed/" rel="self" type="application/rss+xml" />
	<link>https://greyli.com</link>
	<description>一个编程和写作爱好者的在线记事本</description>
	<lastBuildDate>Sat, 15 Nov 2025 10:55:15 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.9.26</generator>

<image>
	<url>https://greyli.com/wp-content/uploads/2025/03/avatar-500-compressed-144x144.jpg</url>
	<title>Tkinter &#8211; 李辉 / Grey Li</title>
	<link>https://greyli.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Tkinter效果总结</title>
		<link>https://greyli.com/effect-of-tkinter-summary/</link>
		<comments>https://greyli.com/effect-of-tkinter-summary/#respond</comments>
		<pubDate>Tue, 16 Aug 2016 14:22:12 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tkinter]]></category>

		<guid isPermaLink="false">http://withlihui.com/?p=891</guid>
		<description><![CDATA[Tkinter是Python附带的标准GUI工具包。它易于使用，但支持的部件较少。其他的比较有名的GUI工具包 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Tkinter是Python附带的标准GUI工具包。它易于使用，但支持的部件较少。其他的比较有名的GUI工具包有wxPytohn，PyQt，PyGTK。下面总结了一些常用的GUI效果的实现方式。</p>
<p>&nbsp;</p>
<h2>Splash</h2>
<p>Splash就是程序启动界面（主界面显示前的界面），比如手机APP打开后显示的LOGO画面（大多是广告），splash通常可用来放品牌logo或公司信息。</p>
<pre class=""># import Tkinter
def splash():
    root = Tkinter.Tk()
    root.overrideredirect(True)
    width = root.winfo_screenwidth()
    height = root.winfo_screenheight()
    root.geometry('%dx%d+%d+%d' % (width*0.8, height*0.8,
                                   width*0.1, height*0.1))
    # 保存成gif格式。（但是没法显示动图，待解决。）
    image_file = "hi.gif"
    #assert os.path.exists(image_file)
    # use Tkinter's PhotoImage for .gif files
    image = Tkinter.PhotoImage(file=image_file)
    canvas = Tkinter.Canvas(root, height=height*0.8, 
                            width=width*0.8, bg="white")
    canvas.create_image(width*0.8/2, height*0.8/2, image=image)
    canvas.pack()
    # 设置splash显示的时间，单位是毫秒（milliseconds）
    root.after(4000, root.destroy)
    root.mainloop()
</pre>
<p>在主程序启动前调用splash()</p>
<div id="attachment_895" style="width: 867px" class="wp-caption aligncenter"><a href="http://greyli.com/wp-content/uploads/2016/08/splash.jpg" rel="attachment wp-att-895"><img class="wp-image-895 size-full" src="http://greyli.com/wp-content/uploads/2016/08/splash.jpg" alt="splash效果" width="857" height="679" srcset="https://greyli.com/wp-content/uploads/2016/08/splash.jpg 857w, https://greyli.com/wp-content/uploads/2016/08/splash-150x119.jpg 150w, https://greyli.com/wp-content/uploads/2016/08/splash-300x238.jpg 300w, https://greyli.com/wp-content/uploads/2016/08/splash-624x494.jpg 624w" sizes="(max-width: 857px) 100vw, 857px" /></a><p class="wp-caption-text">某软件的启动界面（splash）</p></div>
<h2> </h2>
<h2>无边框</h2>
<p>只需要加入下面一行代码。</p>
<pre class="">root.overrideredirect(True)</pre>
<h2> </h2>
<div id="attachment_894" style="width: 566px" class="wp-caption aligncenter"><a href="http://greyli.com/wp-content/uploads/2016/08/noframe.jpg" rel="attachment wp-att-894"><img class="wp-image-894 size-full" src="http://greyli.com/wp-content/uploads/2016/08/noframe.jpg" alt="无边框效果" width="556" height="649" srcset="https://greyli.com/wp-content/uploads/2016/08/noframe.jpg 556w, https://greyli.com/wp-content/uploads/2016/08/noframe-129x150.jpg 129w, https://greyli.com/wp-content/uploads/2016/08/noframe-257x300.jpg 257w" sizes="(max-width: 556px) 100vw, 556px" /></a><p class="wp-caption-text">某个简陋的无边框软件&#8230;&#8230;</p></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2> 多标签面板</h2>
<p>首先定义一个类，建议把它保存成单独的文件，然后把它放在系统环境变量里，这样可以方便调用。</p>
<pre class="">from Tkinter import *
class Notebook(object):
    def __init__(self, master, side=LEFT):
        self.active_fr = None
        self.count = 0
        self.choice = IntVar(0)
        if side in (TOP, BOTTOM): self.side = LEFT
        else: self.side = TOP
        self.rb_fr = Frame(master, borderwidth=2, relief=RIDGE)
        self.rb_fr.pack(side=side, fill=BOTH)
        self.screen_fr = Frame(master, borderwidth=2, relief=RIDGE)
        self.screen_fr.pack(fill=BOTH)
 
    def __call__(self):
        return self.screen_fr
 
    def add_screen(self, fr, title):
        b = Radiobutton(self.rb_fr, text=title, indicatoron=0,
                        variable=self.choice, value=self.count,
                        command=lambda: self.display(fr))
        b.pack(fill=BOTH, side=self.side)
        if not self.active_fr:
             fr.pack(fill=BOTH, expand=1)
             self.active_fr = fr
        self.count += 1
 
    def display(self, fr):
        self.active_fr.forget()
        fr.pack(fill=BOTH, expand=1)
        self.active_fr = fr</pre>
<p>下面是一个示例：</p>
<pre class="">from Tkinter import *
from notebook import * # 如果notebook类在同一个文件就省去这行
# 生成一个顶层窗口，放入notebook
root = Tk()
root.geometry("400x400+500+300")
nb = Notebook(root, TOP) # 标签的位置可选：LEFT、TOP

# 每一个框架（Frame）代表一个标签面板
f1 = Frame(nb())
b1 = Button(f1, text="Button 1")
e1 = Entry(f1)

b1.pack(fill=BOTH, expand=1)
e1.pack(fill=BOTH, expand=1)
# 在将框架添加到记事本之前，先把部件放进去
# 不要单独放入框架

f2 = Frame(nb())
b2 = Button(f2, text="Button 2")
iconImage = Tkinter.PhotoImage(master=f2, data=icon)
Tkinter.Button(image=iconImage).pack()
b3 = Button(f2, image=iconImage)
 
b2.pack(fill=BOTH, expand=1)
b3.pack(fill=BOTH, expand=1)

f3 = Frame(nb())
lb1 = Label(f3, text="欢迎来到标签3...")

lb1.pack()
# 添加标签面板和面板名称
nb.add_screen(f1, "标签 1") 
nb.add_screen(f2, "标签 2") 
nb.add_screen(f3, "标签 3")

root.mainloop() </pre>
<p><a href="http://greyli.com/wp-content/uploads/2016/08/nb.jpg" rel="attachment wp-att-897"><img class="aligncenter size-full wp-image-897" src="http://greyli.com/wp-content/uploads/2016/08/nb.jpg" alt="标签面板示例" width="421" height="445" srcset="https://greyli.com/wp-content/uploads/2016/08/nb.jpg 421w, https://greyli.com/wp-content/uploads/2016/08/nb-142x150.jpg 142w, https://greyli.com/wp-content/uploads/2016/08/nb-284x300.jpg 284w" sizes="(max-width: 421px) 100vw, 421px" /></a></p>
<p>&nbsp;</p>
<h2> </h2>
<h2>新手备注</h2>
<p>在Python中使用文件时要把文件的完整路径写出来。或者在调用前使用os模块的chdir函数。</p>
<pre class="">import os
os.chdir("C:\photos\")
os.chdir(os.getcwd()) # better</pre>
<p>如果要发布给别人用，那就使用相对路径。使用下面这个函数包裹你要使用的文件路径（相对于程序根目录的路径），这样即使你换了文件夹，文件还是会得到正确的调用。</p>
<pre class="">import os
import sys

def resource_path(relative_path):
    """
    定义一个读取相对路径的函数
    1、引用文件用如下格式：resource_path('resources/complete.wav')
    你只需要填入相对于程序根目录的文件路径。
    2、打包exe的说明：在生成的.spec文件exe = EXE()中加入下面这行：
    [('resources/complete.wav',r'C:\Users\Administrator\resources\complete.wav','music'),],
    列表中的三项分别为代码中的引用，文件实际的地址，类别
    这样打包后文件会被正确引用。
    """
    if hasattr(sys, "_MEIPASS"):
        base_path = sys._MEIPASS
    else:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)

</pre>
<p>&nbsp;</p>
<p>注：标签面板的代码来自《Python Cookbook》</p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/effect-of-tkinter-summary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
