繁体   English   中英

Tkinter按钮将不会与画布滚动

[英]Tkinter button won't scroll with canvas

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

问题是,当我向上或向下滚动时,“视图”按钮根本不会移动。

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()

有什么建议吗?

终于找到了解决方案,只需将按钮添加到窗口中,然后将此窗口添加到画布中

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() 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM