python为text添加滚动条,python tkinter滚动条和文本小部件问题

I'm trying to get two text boxes that each have scrollbars. When I try this however:

from Tkinter import *

root = Tk()

s_start = Scrollbar(root)

t_start = Text(root, width=50, height=10)

t_start.focus_set()

s_start.pack(side=RIGHT, fill=Y)

t_start.pack(side=LEFT, fill=Y)

s_start.config(command=t_start.yview)

t_start.config(yscrollcommand=s_start.set)

s_end = Scrollbar(root)

t_end = Text(root, width=50, height=10)

t_end.focus_set()

s_end.pack(side=RIGHT, fill=Y)

t_end.pack(side=LEFT, fill=Y)

s_end.config(command=t_end.yview)

t_end.config(yscrollcommand=s_end.set)

root.mainloop()

This happens:

137288efab5079caa7d612594841c70e.png

In case this isn't clear, those are two separate text boxes, with the right textbox functionally bound to the inner scroll bar, and the left textbox functionally bound to the outer scrollbar.

解决方案

The trick is to use Frames and add the Scrollbars to the Frames instead of to Root.

from Tkinter import *

root = Tk()

left = Frame(root)

right = Frame(root)

t_start = Text(left, width=20)

t_start.pack(side=LEFT, fill=Y)

s_start = Scrollbar(left)

s_start.pack(side=RIGHT, fill=Y)

s_start.config(command=t_start.yview)

t_start.config(yscrollcommand=s_start.set)

t_end = Text(right, width=20)

t_end.pack(side=LEFT, fill=Y)

s_end = Scrollbar(right)

s_end.pack(side=RIGHT, fill=Y)

s_end.config(command=t_end.yview)

t_end.config(yscrollcommand=s_end.set)

left.pack(side=LEFT, fill=Y)

right.pack(side=RIGHT, fill=Y)

root.geometry("500x200")

root.mainloop()

440183f3ef02ebaac901b5ccc0409290.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值