求助:tkinter中滚动条为什么不能用
做一个界面,为什么滚动条不能用?弄了5天了,实在解决不了,求大神帮助,谢谢,代码如下:import tkinter as tk
from tkinter import ttk
def cbBegin(self):
pass
def sel_result():
pass
def get_result():
pass
def get_data(filename):
pass
mb_col = ['A','B','C','D','E','F','G','H','I','J','K']
xx_col = ['A','B','C','D','E']
root = tk.Tk() # 创建一个窗口
root.title('AAAA') # 窗口标题
root.geometry("430x300+370+150")
srb = ttk.Scrollbar(root)
frm = tk.Text(root, height='8', width='20')
global listC
listC = []
tk.Label(root, text='AAAAAAAA', height=1, width=20,\
padx=0,pady=10,background='green', foreground='white').grid(row=0,column=0)
tk.Label(root, text='DDDDDDDD', height=1, width=20,\
padx=0,pady=10,background='green', foreground='white').grid(row=0,column=1)
for i, m in enumerate(mb_col):
lab = ttk.Label(frm, text=m, width=20)
frm.window_create(tk.INSERT,window=lab)
lab.grid(row=i + 1,column=0, padx=5, pady=5, columnspan=2)
cv = tk.StringVar()
cmb = ttk.Combobox(frm, values= xx_col, textvariable=cv, width=30)
frm.window_create(tk.INSERT,window=cmb)
listC.append(cmb)
cmb.grid(row=i + 1, column=4, padx=5, pady=5, columnspan=3)
cmb.bind('<FocusIn>', cbBegin) # 得到焦点时执行
srb.config(command=frm.yview)
frm.config(yscrollcommand=srb.set)
srb.grid(row=1, column=2, rowspan=i, sticky=tk.S + tk.N)
frm.grid(row=1,column=0,columnspan=2, sticky=tk.S + tk.N)
tk.Button(root, text="确定", command=get_result, height=1, width=7, \
padx=10, pady=1, bg='orange').grid(row=i+1,column=0,padx=5, pady=5)
tk.Button(root, text="退出", command=root.quit, height=1, width=7, \
padx=10, pady=1, bg='orange').grid(row=i+1,column=1, padx=5, pady=5)
root.mainloop() 本帖最后由 Twilight6 于 2020-6-3 12:48 编辑
srb = tk.Scrollbar(root)去掉一个t
因为你对 text对象加的滚动条 但是你text 没有文本内容超出进度条就不能用
如图:
还有建议设置 text 只读加上参数 state = DISABLED ,但是就不能拉滚动条了,滚动条个人觉得没有意义
不过可以增大窗口呀?
root.geometry("430x450+370+150")
看你个人 吧
import tkinter as tk
from tkinter import ttk
def cbBegin(self):
pass
def sel_result():
pass
def get_result():
pass
def get_data(filename):
pass
mb_col = ['A','B','C','D','E','F','G','H','I','J','K']
xx_col = ['A','B','C','D','E']
root = tk.Tk() # 创建一个窗口
root.title('AAAA') # 窗口标题
root.geometry("430x450+370+150")
srb = tk.Scrollbar(root)
frm = tk.Text(root, height='8', width='20')
listC = []
tk.Label(root, text='AAAAAAAA', height=1, width=20,\
padx=0,pady=10,background='green', foreground='white').grid(row=0,column=0)
tk.Label(root, text='DDDDDDDD', height=1, width=20,\
padx=0,pady=10,background='green', foreground='white').grid(row=0,column=1)
for i, m in enumerate(mb_col):
lab = ttk.Label(frm, text=m, width=20)
frm.window_create(tk.INSERT,window=lab)
lab.grid(row=i + 1,column=0, padx=5, pady=5, columnspan=2)
cv = tk.StringVar()
cmb = ttk.Combobox(frm, values= xx_col, textvariable=cv, width=30)
frm.window_create(tk.INSERT,window=cmb)
listC.append(cmb)
cmb.grid(row=i + 1, column=4, padx=5, pady=5, columnspan=3)
cmb.bind('<FocusIn>', cbBegin) # 得到焦点时执行
srb.config(command=frm.yview)
frm.config(yscrollcommand=srb.set)
srb.grid(row=1, column=2, rowspan=i, sticky=tk.S + tk.N)
frm.grid(row=1,column=0,columnspan=2, sticky=tk.S + tk.N)
tk.Button(root, text="确定", command=get_result, height=1, width=7, \
padx=10, pady=1, bg='orange').grid(row=i+1,column=0,padx=5, pady=5)
tk.Button(root, text="退出", command=root.quit, height=1, width=7, \
padx=10, pady=1, bg='orange').grid(row=i+1,column=1, padx=5, pady=5)
root.mainloop()
本帖最后由 hyqzzs 于 2020-6-3 14:13 编辑
Twilight6 发表于 2020-6-3 11:49
srb = tk.Scrollbar(root)去掉一个t
因为你对 text对象加的滚动条 但是你text 没有文本内容超出进度条 ...
谢谢你的回答,我是不是可以理解为,text + scrollbar只能对其中的内容进行滚动显示,但是对控件不能控制?事实上添加的标签控件应该达到40个以上,我如果希望能够滚动显示下面的标签应该怎么修改?麻烦指点一下。可以不用text。 hyqzzs 发表于 2020-6-3 14:11
谢谢你的回答,我是不是可以理解为,text + scrollbar只能对其中的内容进行滚动显示,但是对控件不能控 ...
嗯 控件被你grid固定了位置,不固定位置即可 等下我改下代码 hyqzzs 发表于 2020-6-3 14:11
谢谢你的回答,我是不是可以理解为,text + scrollbar只能对其中的内容进行滚动显示,但是对控件不能控 ...
import tkinter as tk
from tkinter import ttk
def cbBegin(self):
pass
def sel_result():
pass
def get_result():
pass
def get_data(filename):
pass
mb_col = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N']
xx_col = ['A','B','C','D','E']
root = tk.Tk() # 创建一个窗口
root.title('AAAA') # 窗口标题
frm = tk.Text(root,height='28', width='61')
listC = []
tk.Label(root, text='AAAAAAAA', height=1, width=20,\
padx=0,pady=10,background='green', foreground='white').grid(row=0,column=0)
tk.Label(root, text='DDDDDDDD', height=1, width=20,\
padx=0,pady=10,background='green', foreground='white').grid(row=0,column=1)
for i, m in enumerate(mb_col):
lab = tk.Label(frm, text=m, width=20)
frm.window_create(tk.INSERT,window=lab)
cv = tk.StringVar()
cmb = ttk.Combobox(frm, values= xx_col, textvariable=cv, width=30)
frm.window_create(tk.INSERT,window=cmb)
listC.append(cmb)
cmb.bind('<FocusIn>', cbBegin) # 得到焦点时执行
frm.insert(tk.END,'\n\n')
srb = tk.Scrollbar()
srb.grid(columnspan=3,sticky=tk.E+tk.N+tk.S)
srb.config(command=frm.yview)
frm.config(yscrollcommand=srb.set,state=tk.DISABLED)
frm.grid(row=1,column=0,columnspan=3, sticky=tk.S + tk.N)
tk.Button(root, text="确定", command=get_result, height=1, width=7, \
padx=10, pady=1, bg='orange').grid(row=i+1,column=0,padx=5, pady=5)
tk.Button(root, text="退出", command=root.quit, height=1, width=7, \
padx=10, pady=1, bg='orange').grid(row=i+1,column=1, padx=5, pady=5)
root.mainloop() Twilight6 发表于 2020-6-3 16:04
十分感谢!正是我想要的效果!{:5_95:}
页:
[1]