鱼C论坛

 找回密码
 立即注册
查看: 2142|回复: 5

[已解决]求助:tkinter中滚动条为什么不能用

[复制链接]
发表于 2020-6-3 11:43:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
做一个界面,为什么滚动条不能用?弄了5天了,实在解决不了,求大神帮助,谢谢,代码如下:

  1. import tkinter as tk
  2. from tkinter import ttk

  3. def cbBegin(self):
  4.     pass
  5. def sel_result():
  6.     pass
  7. def get_result():
  8.     pass
  9. def get_data(filename):
  10.     pass

  11. mb_col = ['A','B','C','D','E','F','G','H','I','J','K']
  12. xx_col = ['A','B','C','D','E']
  13. root = tk.Tk()           # 创建一个窗口
  14. root.title('AAAA')    # 窗口标题
  15. root.geometry("430x300+370+150")   
  16. srb = ttk.Scrollbar(root)
  17. frm = tk.Text(root, height='8', width='20')
  18. global listC
  19. listC = []
  20. tk.Label(root, text='AAAAAAAA', height=1, width=20,  \
  21.     padx=0,pady=10,background='green', foreground='white').grid(row=0,column=0)     
  22. tk.Label(root, text='DDDDDDDD', height=1, width=20,  \
  23.     padx=0,pady=10,background='green', foreground='white').grid(row=0,column=1)
  24. for i, m in enumerate(mb_col):
  25.     lab = ttk.Label(frm, text=m, width=20)
  26.     frm.window_create(tk.INSERT,window=lab)
  27.     lab.grid(row=i + 1,column=0, padx=5, pady=5, columnspan=2)  
  28.     cv = tk.StringVar()
  29.     cmb = ttk.Combobox(frm, values= xx_col, textvariable=cv, width=30)
  30.     frm.window_create(tk.INSERT,window=cmb)
  31.     listC.append(cmb)
  32.     cmb.grid(row=i + 1, column=4, padx=5, pady=5, columnspan=3)
  33.     cmb.bind('<FocusIn>', cbBegin)       # 得到焦点时执行

  34. srb.config(command=frm.yview)
  35. frm.config(yscrollcommand=srb.set)
  36. srb.grid(row=1, column=2, rowspan=i, sticky=tk.S + tk.N)
  37. frm.grid(row=1,column=0,columnspan=2, sticky=tk.S + tk.N)

  38. tk.Button(root, text="确定", command=get_result, height=1, width=7, \
  39.     padx=10, pady=1, bg='orange').grid(row=i+1,column=0,padx=5, pady=5)

  40. tk.Button(root, text="退出", command=root.quit, height=1, width=7, \
  41.     padx=10, pady=1, bg='orange').grid(row=i+1,column=1, padx=5, pady=5)

  42. root.mainloop()
复制代码
最佳答案
2020-6-3 16:04:15
hyqzzs 发表于 2020-6-3 14:11
谢谢你的回答,我是不是可以理解为,text + scrollbar只能对其中的内容进行滚动显示,但是对控件不能控 ...
  1. import tkinter as tk
  2. from tkinter import ttk

  3. def cbBegin(self):
  4.     pass
  5. def sel_result():
  6.     pass
  7. def get_result():
  8.     pass
  9. def get_data(filename):
  10.     pass

  11. mb_col = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N']
  12. xx_col = ['A','B','C','D','E']
  13. root = tk.Tk()           # 创建一个窗口
  14. root.title('AAAA')    # 窗口标题

  15. frm = tk.Text(root,height='28', width='61')

  16. listC = []
  17. tk.Label(root, text='AAAAAAAA', height=1, width=20,  \
  18.     padx=0,pady=10,background='green', foreground='white').grid(row=0,column=0)
  19. tk.Label(root, text='DDDDDDDD', height=1, width=20,  \
  20.     padx=0,pady=10,background='green', foreground='white').grid(row=0,column=1)
  21. for i, m in enumerate(mb_col):
  22.     lab = tk.Label(frm, text=m, width=20)
  23.     frm.window_create(tk.INSERT,window=lab)
  24.     cv = tk.StringVar()
  25.     cmb = ttk.Combobox(frm, values= xx_col, textvariable=cv, width=30)
  26.     frm.window_create(tk.INSERT,window=cmb)
  27.     listC.append(cmb)
  28.     cmb.bind('<FocusIn>', cbBegin)       # 得到焦点时执行
  29.     frm.insert(tk.END,'\n\n')
  30. srb = tk.Scrollbar()
  31. srb.grid(columnspan=3,sticky=tk.E+tk.N+tk.S)
  32. srb.config(command=frm.yview)
  33. frm.config(yscrollcommand=srb.set,state=tk.DISABLED)

  34. frm.grid(row=1,column=0,columnspan=3, sticky=tk.S + tk.N)

  35. tk.Button(root, text="确定", command=get_result, height=1, width=7, \
  36.     padx=10, pady=1, bg='orange').grid(row=i+1,column=0,padx=5, pady=5)

  37. tk.Button(root, text="退出", command=root.quit, height=1, width=7, \
  38.     padx=10, pady=1, bg='orange').grid(row=i+1,column=1, padx=5, pady=5)

  39. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-3 11:49:58 | 显示全部楼层

回帖奖励 +5 鱼币

本帖最后由 Twilight6 于 2020-6-3 12:48 编辑

srb = tk.Scrollbar(root)  去掉一个t

因为你对 text对象加的滚动条 但是你text 没有文本内容超出进度条就不能用

如图:

ttt.gif

还有建议设置 text 只读加上参数 state = DISABLED ,但是就不能拉滚动条了  ,滚动条个人觉得没有意义

不过可以增大窗口呀  ?

  1. root.geometry("430x450+370+150")
复制代码



看你个人 吧

  1. import tkinter as tk
  2. from tkinter import ttk

  3. def cbBegin(self):
  4.     pass
  5. def sel_result():
  6.     pass
  7. def get_result():
  8.     pass
  9. def get_data(filename):
  10.     pass

  11. mb_col = ['A','B','C','D','E','F','G','H','I','J','K']
  12. xx_col = ['A','B','C','D','E']
  13. root = tk.Tk()           # 创建一个窗口
  14. root.title('AAAA')    # 窗口标题
  15. root.geometry("430x450+370+150")
  16. srb = tk.Scrollbar(root)
  17. frm = tk.Text(root, height='8', width='20')

  18. listC = []
  19. tk.Label(root, text='AAAAAAAA', height=1, width=20,  \
  20.     padx=0,pady=10,background='green', foreground='white').grid(row=0,column=0)
  21. tk.Label(root, text='DDDDDDDD', height=1, width=20,  \
  22.     padx=0,pady=10,background='green', foreground='white').grid(row=0,column=1)
  23. for i, m in enumerate(mb_col):
  24.     lab = ttk.Label(frm, text=m, width=20)
  25.     frm.window_create(tk.INSERT,window=lab)
  26.     lab.grid(row=i + 1,column=0, padx=5, pady=5, columnspan=2)
  27.     cv = tk.StringVar()
  28.     cmb = ttk.Combobox(frm, values= xx_col, textvariable=cv, width=30)
  29.     frm.window_create(tk.INSERT,window=cmb)
  30.     listC.append(cmb)
  31.     cmb.grid(row=i + 1, column=4, padx=5, pady=5, columnspan=3)
  32.     cmb.bind('<FocusIn>', cbBegin)       # 得到焦点时执行

  33. srb.config(command=frm.yview)
  34. frm.config(yscrollcommand=srb.set)
  35. srb.grid(row=1, column=2, rowspan=i, sticky=tk.S + tk.N)
  36. frm.grid(row=1,column=0,columnspan=2, sticky=tk.S + tk.N)

  37. tk.Button(root, text="确定", command=get_result, height=1, width=7, \
  38.     padx=10, pady=1, bg='orange').grid(row=i+1,column=0,padx=5, pady=5)

  39. tk.Button(root, text="退出", command=root.quit, height=1, width=7, \
  40.     padx=10, pady=1, bg='orange').grid(row=i+1,column=1, padx=5, pady=5)

  41. root.mainloop()
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-3 14:11:13 | 显示全部楼层
本帖最后由 hyqzzs 于 2020-6-3 14:13 编辑
Twilight6 发表于 2020-6-3 11:49
srb = tk.Scrollbar(root)  去掉一个t

因为你对 text对象加的滚动条 但是你text 没有文本内容超出进度条 ...


谢谢你的回答,我是不是可以理解为,text + scrollbar只能对其中的内容进行滚动显示,但是对控件不能控制?事实上添加的标签控件应该达到40个以上,我如果希望能够滚动显示下面的标签应该怎么修改?麻烦指点一下。可以不用text。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-3 15:25:55 | 显示全部楼层
hyqzzs 发表于 2020-6-3 14:11
谢谢你的回答,我是不是可以理解为,text + scrollbar只能对其中的内容进行滚动显示,但是对控件不能控 ...

嗯 控件被你grid固定了位置,不固定位置即可 等下我改下代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-3 16:04:15 | 显示全部楼层    本楼为最佳答案   
hyqzzs 发表于 2020-6-3 14:11
谢谢你的回答,我是不是可以理解为,text + scrollbar只能对其中的内容进行滚动显示,但是对控件不能控 ...
  1. import tkinter as tk
  2. from tkinter import ttk

  3. def cbBegin(self):
  4.     pass
  5. def sel_result():
  6.     pass
  7. def get_result():
  8.     pass
  9. def get_data(filename):
  10.     pass

  11. mb_col = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N']
  12. xx_col = ['A','B','C','D','E']
  13. root = tk.Tk()           # 创建一个窗口
  14. root.title('AAAA')    # 窗口标题

  15. frm = tk.Text(root,height='28', width='61')

  16. listC = []
  17. tk.Label(root, text='AAAAAAAA', height=1, width=20,  \
  18.     padx=0,pady=10,background='green', foreground='white').grid(row=0,column=0)
  19. tk.Label(root, text='DDDDDDDD', height=1, width=20,  \
  20.     padx=0,pady=10,background='green', foreground='white').grid(row=0,column=1)
  21. for i, m in enumerate(mb_col):
  22.     lab = tk.Label(frm, text=m, width=20)
  23.     frm.window_create(tk.INSERT,window=lab)
  24.     cv = tk.StringVar()
  25.     cmb = ttk.Combobox(frm, values= xx_col, textvariable=cv, width=30)
  26.     frm.window_create(tk.INSERT,window=cmb)
  27.     listC.append(cmb)
  28.     cmb.bind('<FocusIn>', cbBegin)       # 得到焦点时执行
  29.     frm.insert(tk.END,'\n\n')
  30. srb = tk.Scrollbar()
  31. srb.grid(columnspan=3,sticky=tk.E+tk.N+tk.S)
  32. srb.config(command=frm.yview)
  33. frm.config(yscrollcommand=srb.set,state=tk.DISABLED)

  34. frm.grid(row=1,column=0,columnspan=3, sticky=tk.S + tk.N)

  35. tk.Button(root, text="确定", command=get_result, height=1, width=7, \
  36.     padx=10, pady=1, bg='orange').grid(row=i+1,column=0,padx=5, pady=5)

  37. tk.Button(root, text="退出", command=root.quit, height=1, width=7, \
  38.     padx=10, pady=1, bg='orange').grid(row=i+1,column=1, padx=5, pady=5)

  39. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-6-3 16:12:59 | 显示全部楼层

十分感谢!正是我想要的效果!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 04:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表