python tkinter滚动条和文本小部件问题 [英] python tkinter scrollbar and text widget issues

查看:525
本文介绍了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()

发生这种情况:

如果不清楚,则它们是两个单独的文本框,右侧的文本框功能性地绑定到内部滚动条,而左侧的文本框功能性地绑定到外部滚动条.

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.

推荐答案

诀窍是使用Frames并将滚动条添加到Frames而不是Root中.

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()

这篇关于python tkinter滚动条和文本小部件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆