python - 手动配置滚动条的 Y 尺寸

标签 python python-3.x tkinter

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

我正在创建一个简单的 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)

谢谢! 此外,任何建议都会受到赞赏。 :)

最佳答案

将 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/

相关文章:

python - Django : Timeout when reading response headers from daemon process 的 mod_wsgi 错误

python - 如何将元组转换为命名元组?

python - 如何选择目录并将其存储到 tkinter 中的变量中?

python - 如何根据按钮点击无条件重新运行python程序?

python - 如何在 Python 的 matplotlib 中设置 'backend'?

python - 发布项目沃尔玛合作伙伴 API Python

python - 我怎样才能得到这个页面的内容?

python - Tastypie 与 application/x-www-form-urlencoded

python - 有没有办法用 Pillow 仅反转特定像素?

python - 如何将焦点放在 python Tkinter 文本小部件上?