让我介绍一下我想做的事情。
我正在创建一个简单的 Tkinter 程序,它使用 Treeview 小部件为用户显示信息。
我一开始使用的是 .grid(),但现在我觉得使用 .place() 更舒服。这里的问题是滚动条。 It doesn't fill the Y-Dimension就像 .pack() 一样。
作为奖励,this happens当我使用 .pack() 时
这是代码,如果您需要的话:
self.ah = Treeview(self.M, selectmode='browse')
self.ah["columns"] = ("id", "time", "pr")
#
self.ah.heading('#0', text='Description', anchor='c')
self.ah.column('#0', anchor='c', width=170)
#
self.ah.heading('id', text='ID', anchor='c')
self.ah.column('id', anchor='c', width=30)
#
self.ah.heading('time', text='time', anchor='c')
self.ah.column('time', anchor='c', width=100)
#
self.ah.heading('pr', text='stuff', anchor='c')
self.ah.column('pr', anchor='c', width=70)
#
self.ah.place(x=400, y=70)
#
self.ah.scroll = Scrollbar(self.M, orient='vertical', command=self.ah.yview)
#
self.ah.scroll.place(x=770, y=70)
self.ah.config(yscrollcommand=self.ah.scroll.set)
谢谢! 此外,任何建议都会受到赞赏。 :)
最佳答案
将 Treeview 和 Scrollbar 打包到一个 Frame 中,然后使用 place 将 Frame 放置在您想要的位置。像这样:
self.ah_frame = Frame(self.M)
self.ah = Treeview(self.ah_frame, selectmode='browse')
self.ah.pack(side=LEFT)
self.ah.scroll = Scrollbar(self.ah_frame, orient='vertical', command=self.ah.yview)
self.ah.scroll.pack(side=RIGHT, fill=Y, expand=True)
self.ah_frame.place(x=400, y=70) # place the Frame that contains both the Treeview and Scrollbar
此外,我强烈建议您尽可能避免地点
。小部件会随着不同的用户设置、字体和操作系统而改变大小。使用位置意味着您的代码在您的计算机上看起来像您想要的样子,但在其他计算机上却不是。
关于python - 手动配置滚动条的 Y 尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47873982/