简体   繁体   English

Tkinter 滚动条不可滚动

[英]Tkinter scrollbar not scrollable

I followed some tutorial on attaching a scrollbar to a textbox.我遵循了一些关于将滚动条附加到文本框的教程。 However, in the tutorial, the scrollbar is really a "bar".但是,在教程中,滚动条实际上​​是一个“条”。 When I tried myself, I can only press the arrows to move up or down, the middle part is not movable.自己试了一下,只能按箭头上下移动,中间部分不可动。 May I know what I did wrong?我可以知道我做错了什么吗?

import tkinter as tk

root = tk.Tk()
scroll = tk.Scrollbar(root)
scroll.grid(row = 0, column = 1)
message = tk.Text(root, yscrollcommand = scroll.set, height = 25, width = 60)
message.grid(row = 0, column = 0)
for i in range(50):
    message.insert(tk.END, f'This is line {i}\n')
scroll.config(command = message.yview)

root.mainloop()

代码生成的tk窗口

You just have to add sticky='nsew' to Scrollbar widget.您只需将sticky='nsew'添加到Scrollbar小部件。

sticky='nsew' will make the Scrollbar widget to expand to fill up the entire cell (at grid position row=0 & column=1 ) at every side (n- north , s- south , e- east , w- west ) sticky='nsew'将使Scrollbar小部件在每一侧(n-,s-,e-,w-西)扩展以填满整个单元格(在网格位置row=0 & column=1

Here is the code:这是代码:

import tkinter as tk

root = tk.Tk()
scroll = tk.Scrollbar(root)

# add sticky option to the Scrollbar widget
scroll.grid(row = 0, column = 1, sticky='nsew')

message = tk.Text(root, yscrollcommand = scroll.set, height = 25, width = 60)
message.grid(row = 0, column = 0)
for i in range(50):
    message.insert(tk.END, f'This is line {i}\n')
scroll.config(command = message.yview)

root.mainloop()

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

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