Tkinter学习之路-------如何加入X轴滚动

小伙伴们在学习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()

之后我们在运行代码:

就得到了可以横向滑动的滚动条了。 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值