【问题标题】:Python can't add canvas to ttk notebook pagePython 无法将画布添加到 ttk 笔记本页面
【发布时间】:2016-08-29 13:11:39
【问题描述】:
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加点东西?

【问题讨论】:

  • 如果你想把画布放在笔记本中,你必须把它放在 in 笔记本中。现在它在self,就在笔记本下面。
  • 我怎样才能把它装在笔记本里?我不太确定该怎么做。

标签: python tkinter polygon ttk tkinter-canvas


【解决方案1】:

如果您希望画布位于笔记本中,则必须将其添加到笔记本页面之一。例如,如果您希望它在 self.page 中,您可以更改此行:

canvas = Canvas(self)

...到这个:

canvas = Canvas(self.page1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 2023-03-23
    • 1970-01-01
    • 2016-09-16
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多