Tkinter按钮无法与画布一起滚动

2024-10-06 11:17:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码,一个简单的窗口,有一个画布和一些按钮,一个在画布内,另一个在它下面。在

问题是,当我向上或向下滚动时,“视图”按钮根本就不动,而它本该移动的

def initUI(self):
    self.content = Canvas()
    self.parent.title("window")
    self.style = Style()
    self.style.theme_use("default")

    self.content.pack(fill=BOTH, expand=1)

    self.vbar=Scrollbar(self,orient=VERTICAL)
    self.vbar.pack(side=RIGHT,fill=Y)
    self.vbar.config(command=self.content.yview)
    self.content.config(yscrollcommand=self.vbar.set)

    outButton = Button(self, text="I'm out", command=lambda: self.buttonOut())
    outButton.pack(side=RIGHT)

    view = Button(self.content, text="View Profile", command=someting)
    view.place(x=235, y=160)

    self.pack()

有什么建议吗?在


Tags: textselfrightconfigstyle画布buttoncontent
1条回答
网友
1楼 · 发布于 2024-10-06 11:17:47

终于找到了一个解决方案,只需将按钮添加到一个窗口中,并将此窗口添加到画布上

def initUI(self):
    self.content = Canvas()
    self.parent.title("window")
    self.style = Style()
    self.style.theme_use("default")

    self.content.pack(fill=BOTH, expand=1)

    self.vbar=Scrollbar(self,orient=VERTICAL)
    self.vbar.pack(side=RIGHT,fill=Y)
    self.vbar.config(command=self.content.yview)
    self.content.config(yscrollcommand=self.vbar.set)

    outButton = Button(self, text="I'm out", command=lambda: self.buttonOut())
    outButton.pack(side=RIGHT)
    view = Button(self.content, text="View Profile", command=someting)
-   view.place(x=235, y=160)
+   button_window = self.content.create_window(235, 160, window=view)

    self.pack() 

相关问题 更多 >