Python 无法将 canvas 添加到 ttk 笔记本页面
Python can't add canvas to ttk notebook page
from tkinter import *
from tkinter import ttk
class MainGame(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
global canvas
# ===Part A ===
self.parent.title('PythonPage')
self.pack(fill = BOTH, expand = 1)
self.page = ttk.Notebook(self, width = 650 ,height = 630)
self.page1 = ttk.Frame(self)
self.page.add(self.page1, text = 'Tab1')
self.page.pack(expand = 1, anchor = 'nw', side = 'top')
# ===Part B ===
canvas = Canvas(self)
canvas.create_rectangle([10,10, 650,630], fill = 'blue')
canvas.pack(fill = BOTH, expand = 1)
canvas.update()
self.a = Label(self, text = 'Haha')
self.a.place(x=50,y=50)
root = Tk()
root.geometry('925x650')
main = MainGame(root)
root.mainloop()
如何将我的矩形添加到 ttk 的笔记本中?我发现我的矩形总是创建在notebook下面,但是这种情况和Label
不一样。
我想把长方形放在笔记本里面,要不要加点什么self.page1
?
如果您希望 canvas 出现在笔记本中,您必须将其添加到笔记本页面之一。例如,如果您想要它在 self.page
中,您可以更改此行:
canvas = Canvas(self)
...为此:
canvas = Canvas(self.page1)
from tkinter import *
from tkinter import ttk
class MainGame(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
global canvas
# ===Part A ===
self.parent.title('PythonPage')
self.pack(fill = BOTH, expand = 1)
self.page = ttk.Notebook(self, width = 650 ,height = 630)
self.page1 = ttk.Frame(self)
self.page.add(self.page1, text = 'Tab1')
self.page.pack(expand = 1, anchor = 'nw', side = 'top')
# ===Part B ===
canvas = Canvas(self)
canvas.create_rectangle([10,10, 650,630], fill = 'blue')
canvas.pack(fill = BOTH, expand = 1)
canvas.update()
self.a = Label(self, text = 'Haha')
self.a.place(x=50,y=50)
root = Tk()
root.geometry('925x650')
main = MainGame(root)
root.mainloop()
如何将我的矩形添加到 ttk 的笔记本中?我发现我的矩形总是创建在notebook下面,但是这种情况和Label
不一样。
我想把长方形放在笔记本里面,要不要加点什么self.page1
?
如果您希望 canvas 出现在笔记本中,您必须将其添加到笔记本页面之一。例如,如果您想要它在 self.page
中,您可以更改此行:
canvas = Canvas(self)
...为此:
canvas = Canvas(self.page1)