1

我试图让一个简单的滚动条显示在文本小部件上......我已经派生了以下代码,它添加了滚动条但似乎完全消除了文本小部件。

eula = Tkinter.Text(screen1_2)
eula.insert("1.0", "text")
eula.pack(side="left",fill="both",expand=True)
scroll = Tkinter.Scrollbar(eula)
scroll.pack(side="right",fill="y",expand=False)
4

1 回答 1

3

请注意,您必须yscrollcommand=scroll.set在 Text 小部件中定义,并使用以下行配置滚动条:scroll.config(command=eula.yview) 这是您的代码,用一个滚动条重写:

# Import Tkinter
from Tkinter import *

# define master
master = Tk()

# Vertical (y) Scroll Bar
scroll = Scrollbar(master)
scroll.pack(side=RIGHT, fill=Y)

# Text Widget
eula = Text(master, wrap=NONE, yscrollcommand=scroll.set)
eula.insert("1.0", "text")
eula.pack(side="left")

# Configure the scrollbars
scroll.config(command=eula.yview)
mainloop()

这是一个带有两个滚动条的 Tkinter 示例:

# Import Tkinter
from Tkinter import *

# define master
master = Tk()

# Horizontal (x) Scroll bar
xscrollbar = Scrollbar(master, orient=HORIZONTAL)
xscrollbar.pack(side=BOTTOM, fill=X)

# Vertical (y) Scroll Bar
yscrollbar = Scrollbar(master)
yscrollbar.pack(side=RIGHT, fill=Y)

# Text Widget
text = Text(master, wrap=NONE,
            xscrollcommand=xscrollbar.set,
            yscrollcommand=yscrollbar.set)
text.pack()

# Configure the scrollbars
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)

# Run tkinter main loop
mainloop()
于 2013-10-29T00:01:54.007 回答