我想创建一个基于tkinter的GUI程序。其中一个小部件是Text
。我想在其中添加一个水平滚动条,但它不起作用。
我错在哪里了?
from Tkinter import *
import tkFont
class DpWin(object):
def run(self):
root=Tk()
root.geometry('768x612')
title='dp'
root.title(title)
xscrollbar = Scrollbar(root, orient=HORIZONTAL)
xscrollbar.pack(side=BOTTOM, fill=X)
yscrollbar = Scrollbar(root)
yscrollbar.pack(side=RIGHT, fill=Y)
text = Text(root,xscrollcommand=xscrollbar.set,yscrollcommand=yscrollbar.set)
text.pack()
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)
text.insert(END,'a'*999)
mainloop()
def start(self):
self.b_start.config(state=DISABLED)
self.b_stop.config(state=ACTIVE)
def stop(self):
self.b_stop.config(state=DISABLED)
self.b_start.config(state=ACTIVE)
if __name__=='__main__':
win=DpWin()
win.run()
我已经根据here修改了您的代码。主要有两个不同点。
我这样做是为了不让文本框包装起来。如果换行文本,则水平滚动条没有可滚动的内容。
我使用框架上的网格几何管理器将滚动条和文本小部件放在一起。使用
.grid
的好处是您实际上得到了正确宽度/高度的滚动条(这是使用pack
无法实现的)。。。。
有一条关于让x和y滚动条在pack框架中工作的评论。下面是一个简单的例子:
相关问题 更多 >
编程相关推荐