小伙伴们在学习tkinter模块中是否遇到这样的情况:
import tkinter as tk
root = tk.Tk()
#设置标题
root.title("在文本输入框中加入gundotiao")
#设置主窗口规格
root.geometry('500x400+50+50')
#创建文本输入框对象
entry = tk.Entry(root)
entry.pack(fill='both', expand=1)
#向文本输入框中添加数字填充
for i in range(100000, 100100):
entry.insert('end', str(i))
#创建X轴滚动条
scrollbar = tk.Scrollbar(entry, command=entry.xview)
scrollbar.pack(side='bottom', fill='x')
#将文本输入框与滚动条绑定
entry['xscrollcommand'] = scrollbar.set
root.mainloop()
当我们运行的时候会发生这种情况:
我不是要设置X轴滚动条吗?为什么箭头没有横向,而且只能通过点击箭头才能能像移动文本框里的值。
这是因为在Scrollbar中少了一个参数orient(该参数用来改变滚动条反向,有'horizontal'(水平)和'vertical'(垂直)两个值), 当我们加入这个参数后:
import tkinter as tk
root = tk.Tk()
#设置标题
root.title("在文本输入框中加入gundotiao")
#设置主窗口规格
root.geometry('500x400+50+50')
#创建文本输入框对象
entry = tk.Entry(root)
entry.pack(fill='both', expand=1)
#向文本输入框中添加数字填充
for i in range(100000, 100100):
entry.insert('end', str(i))
#创建X轴滚动条
scrollbar = tk.Scrollbar(entry, command=entry.xview,orient='horizontal')
scrollbar.pack(side='bottom', fill='x')
#将文本输入框与滚动条绑定
entry['xscrollcommand'] = scrollbar.set
root.mainloop()
之后我们在运行代码:
就得到了可以横向滑动的滚动条了。