我想在Tk的一个窗口,有一个自定义标题栏和框架。 我看到这个网页上有关这诸多问题,但我正在寻找的是使用帆布实际呈现的框架,然后将内容添加到画布。 我不能用一个框架来做到这一点,因为边框渐变色。
根据这个网站: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_window-method ,我不能把任何其他帆布项目上的小工具(使用的顶部create_window
方法),但我需要这样做,因为我的一些小部件的使用画布渲染。
任何建议如何做到这一点? 我无言以对这里。
编辑:布莱恩·奥克利证实,用帆布渲染是不可能的。 难道然后才有可能有一个自定义边框颜色的框架? 如果是的话,可能会有人给一个简单的例子? 我还算是有蟒蛇的新。
您可以使用画布就好像它是为了自己绘制窗口边框的框架。 就像你说的,但是,你不能画上嵌入在画布部件的顶部帆布物品; 部件总是拥有最高的堆叠顺序。 周围有没有办法,虽然目前还不清楚,如果你真的需要做的或没有。
这里有一个快速和肮脏的例子来说明如何创建一个窗口自定义边框的渐变。 为了使这个示例总之我没有添加任何代码,让您移动或调整窗口的大小。 同时,它采用了梯度的固定的颜色。
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()
这里是其中帧,标题栏和关闭按钮与帆布矩形制成的粗糙例如:
import Tkinter as tk
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
# Get rid of the os' titlebar and frame
self.overrideredirect(True)
self.mCan = tk.Canvas(self, height=768, width=768)
self.mCan.pack()
# Frame and close button
self.lFrame = self.mCan.create_rectangle(0,0,9,769,
outline='lightgrey', fill='lightgrey')
self.rFrame = self.mCan.create_rectangle(760,0,769,769,
outline='lightgrey', fill='lightgrey')
self.bFrame = self.mCan.create_rectangle(0,760,769,769,
outline='lightgrey', fill='lightgrey')
self.titleBar = self.mCan.create_rectangle(0,0,769,20,
outline='lightgrey', fill='lightgrey')
self.closeButton = self.mCan.create_rectangle(750,4,760, 18,
activefill='red', fill='darkgrey')
# Binds
self.bind('<1>', self.left_mouse)
self.bind('<Escape>', self.close_win)
# Center the window
self.update_idletasks()
xp = (self.winfo_screenwidth() / 2) - (self.winfo_width() / 2)
yp = (self.winfo_screenheight() / 2) - (self.winfo_height() / 2)
self.geometry('{0}x{1}+{2}+{3}'.format(self.winfo_width(),
self.winfo_height(),
xp, yp))
def left_mouse(self, event=None):
obj = self.mCan.find_closest(event.x,event.y)
if obj[0] == self.closeButton:
self.destroy()
def close_win(self, event=None):
self.destroy()
app = Application()
app.mainloop()
如果我要做出一个自定义GUI框架,我会考虑用图像创建它,
用Photoshop等程序制成的,而不是呈现的画布对象。
图像可以放置这样的画布上:
self.ti = tk.PhotoImage(file='test.gif')
self.aImage = mCanvas.create_image(0,0, image=self.ti,anchor='nw')
更多信息→←这里