手动配置滚动条的Y尺寸

问题描述 投票:1回答:1

让我介绍一下我想做的事情。

我正在创建一个简单的Tkinter程序,它使用Treeview小部件来显示用户的信息。

我最初使用.grid(),但现在使用.place()感觉更舒服。这里的问题是滚动条。 It doesn't fill the Y-Dimension和.pack()一样。

相反,I want to have this.

作为奖励,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)

谢谢!此外,任何suggerence赞赏。 :)

python python-3.x tkinter
1个回答
2
投票

将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

此外,我强烈建议你尽可能避免使用place。窗口小部件使用不同的用户设置,字体和操作系统更改大小。使用位置意味着您的代码看起来像您希望它在您的计算机上的方式,而不是其他人。

© www.soinside.com 2019 - 2024. All rights reserved.