【问题标题】:Tkinter button won't scroll with canvasTkinter 按钮不会随画布滚动
【发布时间】:2014-07-12 04:22:01
【问题描述】:

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

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

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

有什么建议吗?

【问题讨论】:

    标签: python python-2.7 canvas tkinter


    【解决方案1】:

    终于找到了解决办法,只需要将按钮添加到一个窗口,并将这个窗口添加到画布上

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

    【讨论】:

      猜你喜欢
      • 2015-12-18
      • 1970-01-01
      • 2017-10-05
      • 2018-07-31
      • 1970-01-01
      • 2021-08-16
      • 2017-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多