這裏是一個粗略的例子,其中的框架,標題欄和關閉按鈕都用帆布矩形制作:
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')
更多信息→here←
我不明白的問題,你的編輯使它更加明確。是的,可以使用畫布創建帶有自定義邊框的框架。這與在小部件頂部繪製畫布項無關 - 這是兩個不同的問題。 – 2012-08-10 01:53:38
我以爲有bordercolor屬性......這不適用於頂級框架,對吧? – MiJyn 2012-08-10 02:06:44
我沒有確認「用畫布渲染是不可能的」。我只是確認,您不能在畫布中嵌入的小部件頂部繪製畫布項目。當然,您可以使用畫布呈現邊框和自定義標題欄。 – 2012-08-10 02:12:01