Tkinter自定义窗口 [英] Tkinter custom window
问题描述
我想在Tk中创建一个具有自定义标题栏和框架的窗口.我已经在这个网站上看到很多与此相关的问题,但是我要寻找的是实际使用画布渲染框架,然后将内容添加到画布.由于边框是渐变的,因此我无法使用框架来完成此操作.
I want to make a window in Tk that has a custom titlebar and frame. I have seen many questions on this website dealing with this, but what I'm looking for is to actually render the frame using a canvas, and then to add the contents to the canvas. I cannot use a frame to do this, as the border is gradiented.
根据此网站: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_window-method ,我不能将任何其他画布项目放在小部件顶部(使用create_window
方法),但是我需要这样做,因为我的某些小部件使用画布渲染.
According to this website: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_window-method, I cannot put any other canvas items on top of a widget (using the create_window
method), but I need to do so, as some of my widgets are rendered using a canvas.
有关如何执行此操作的任何建议?我在这里一无所知.
Any suggestions on how to do this? I'm clueless here.
Bryan Oakley确认使用画布进行渲染是不可能的.这样是否可以使用带有自定义边框颜色的框架?如果是这样,有人可以举个简单的例子吗?我对python很陌生.
Bryan Oakley confirmed that rendering with a canvas would be impossible. Would it then be possible to have a frame with a custom border color? And if so, could someone give a quick example? I'm sort of new with python.
推荐答案
您可以像使用画布一样使用画布来绘制自己的窗口边框.但是,就像您说的那样,您不能在嵌入在画布中的窗口小部件上绘制画布项目.窗口小部件始终具有最高的堆叠顺序.尽管尚不清楚您是否真的需要这样做,但没有办法解决.
You can use the canvas as if it were a frame in order to draw your own window borders. Like you said, however, you cannot draw canvas items on top of widgets embedded in a canvas; widgets always have the highest stacking order. There is no way around that, though it's not clear if you really need to do that or not.
这是一个简单又肮脏的示例,展示了如何为自定义边框创建带有渐变的窗口.为了使示例简短,我没有添加任何代码来允许您移动或调整窗口大小.此外,它为渐变使用固定颜色.
Here's a quick and dirty example to show how to create a window with a gradient for a custom border. To keep the example short I didn't add any code to allow you to move or resize the window. Also, it uses a fixed color for the gradient.
import Tkinter as tk
class GradientFrame(tk.Canvas):
'''A gradient frame which uses a canvas to draw the background'''
def __init__(self, parent, borderwidth=1, relief="sunken"):
tk.Canvas.__init__(self, parent, borderwidth=borderwidth, relief=relief)
self._color1 = "red"
self._color2 = "black"
self.bind("<Configure>", self._draw_gradient)
def _draw_gradient(self, event=None):
'''Draw the gradient'''
self.delete("gradient")
width = self.winfo_width()
height = self.winfo_height()
limit = width
(r1,g1,b1) = self.winfo_rgb(self._color1)
(r2,g2,b2) = self.winfo_rgb(self._color2)
r_ratio = float(r2-r1) / limit
g_ratio = float(g2-g1) / limit
b_ratio = float(b2-b1) / limit
for i in range(limit):
nr = int(r1 + (r_ratio * i))
ng = int(g1 + (g_ratio * i))
nb = int(b1 + (b_ratio * i))
color = "#%4.4x%4.4x%4.4x" % (nr,ng,nb)
self.create_line(i,0,i,height, tags=("gradient",), fill=color)
self.lower("gradient")
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.wm_overrideredirect(True)
gradient_frame = GradientFrame(self)
gradient_frame.pack(side="top", fill="both", expand=True)
inner_frame = tk.Frame(gradient_frame)
inner_frame.pack(side="top", fill="both", expand=True, padx=8, pady=(16,8))
b1 = tk.Button(inner_frame, text="Close",command=self.destroy)
t1 = tk.Text(inner_frame, width=40, height=10)
b1.pack(side="top")
t1.pack(side="top", fill="both", expand=True)
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
这篇关于Tkinter自定义窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!