在Tkin中将Listbox类从根目录移动到顶层时松开yScroll

2024-05-19 15:38:46 发布

您现在位置:Python中文网/ 问答频道 /正文

当listbox是root时,这可以正常工作,但是当我将它移到顶层窗口时,滚动条不再出现

以下是具体代码(注意aassign查询尚未应用)

class App:

def __init__(self):

    self.listb=Toplevel()
    self.listb.transient(root)
    self.listb.title=('DB View')
    self.vsb = Scrollbar(orient="vertical", command=self.OnVsb)
    self.listNum = Listbox(self.listb, yscrollcommand=self.vsb.set)
    self.listRoster = Listbox(self.listb, yscrollcommand=self.vsb.set)
    self.vsb.pack(side="right",fill="y")
    self.listNum.pack(side="left",fill="x", expand=True)
    self.listRoster.pack(side="left",fill="x", expand=True)
    self.listNum.bind("<MouseWheel>", self.OnMouseWheel)
    self.listRoster.bind("<MouseWheel>", self.OnMouseWheel)
    dbi = mdb.connect("localhost", port=3306, user="user", passwd="access", db="interactive_db")
    cursor = dbi.cursor()
    cursor.execute("""SELECT num FROM active_roster""")
    rows = cursor.fetchall()
    cursor.execute("""SELECT firstname, surname, assign FROM active_roster""")
    staff =cursor.fetchall()
    cursor.execute("""SELECT assign FROM active_assign""")
    aassign = cursor.fetchall()
    dbi.close()
    print(rows)
    print(aassign)
    print (staff)

    for results in rows:
        self.listNum.insert("end", results)
    for results2 in staff:
            self.listRoster.insert("end", results2)
    self.listb.mainloop()

def OnVsb(self, *args):
    self.listNum.yview(*args)
    self.listRoster.yview(*args)

def OnMouseWheel(self, event):
    self.listNum.yview("scroll", event.delta,"units")
    self.listRoster.yview("scroll",event.delta,"units")

    return "break"



root = Tk()  
root.title("Main")

root.geometry("900x600")


app=App()
listb = MultipleScrollingListbox()

#PRE-DUAL COLUMN SYNTAX PRE-CLASS DEF

numLabel=Label(root, text="Num #")
numLabel.grid(row=0,column=0)

assLabel=Label(root, text="Assignment")
assLabel.grid(row=0,column=2)


num_input=StringVar()
num_input=Entry(root,textvariable=num_input)
num_input.grid(row=0,column=1)

ass_input=StringVar()
ass_input=Entry(root,textvariable=ass_input)
ass_input.grid(row=0,column=3)


rosterList=Listbox(root, height=6,width=65)
rosterList.grid(row=2, column=0, rowspan=9, columnspan=4)
rosterList.bind('<<ListboxSelect>>', on_selection)

 commScroll=Scrollbar(root)
 commScroll.grid(row=2, column=4, rowspan=9)

 rosterList.configure(yscrollcommand=commScroll.set)
 commScroll.configure(command=rosterList.yview)


root.mainloop() 

我包括了我原来的语法,然后我需要移动到一个多列布局,希望保持相同的外观,我开始迁移到def类-事情开始变得新的错误

我是Tkinter的新手,在这里需要一些指导,因为OnVsb的定义似乎有点混乱

LISTBOX IMAGE


Tags: selfinputdefcolumnrootcursornumgrid
1条回答
网友
1楼 · 发布于 2024-05-19 15:38:46

您需要在滚动条调用中包含主小部件:

self.vsb = Scrollbar(self.listb, orient="vertical", command=self.OnVsb)

否则它默认为第一个根,这就是它以前工作的原因

相关问题 更多 >