简体   繁体   English

使用画布添加到框架的滚动条无法正常工作,只是滚动一点(tkinter)

[英]Scrollbar added to the frame by using canvas doesnt work properly, just scrolls a little bit (tkinter)

my scrollbar doesnt work properly.我的滚动条不能正常工作。 it just scrolls a little bit.它只是滚动一点。 The logic in here is that the worker will select a machine name and than select how many different parts will be used in that machine.这里的逻辑是工人将选择一个机器名称,然后选择该机器将使用多少个不同的部件。 Then there opens entry and comboboxes.然后打开条目和组合框。 If machine includes more than 9 parts than the worker will scroll the frame with scroll bar and enter the 10th and more parts.如果机器包含超过 9 个零件,工人将使用滚动条滚动框架并输入第 10 个或更多零件。

My code is this:我的代码是这样的:

class owindow:
def __init__(self, window):
    
    self.window = window
    self.window.title('SİPARİŞLER')
    self.window.state('zoomed')
    self.window.protocol("WM_DELETE_WINDOW", on_closing)
    
    #Frames
    self.entry = Frame(self.window)
    self.view = Frame(self.window)
    self.m1st = Frame(self.window)
    
    #Labels
    self.nlabel= Label(self.entry,text='Sipariş Veren(*): ', font=('Verdana',12))
    self.mlabel = Label(self.entry, text ='Sipariş Kaç Farklı Makine İçeriyor(*): ',font=('Verdana',12))
    
    #ComboBoxes
    self.centry= ttk.Combobox(self.entry,width=25, values = cname, font = ('Verdana',12))
    
    #EntryBoxes
    self.mqentry = Entry(self.entry, width = 11, font = ('Verdana',12))
    self.orderid = Entry(self.entry)
    
    #Buttons
    self.backbutton = Button(self.window,text='Ana Pencere',font=('Verdana',16),command = self.goback)
    self.add = Button(self.entry, text = 'Sipariş Ekle', font =('Verdana',12),bg ='chartreuse1', command = self.add_order)
    self.delete = Button(self.entry, text ='Sipariş Sil', font =('Verdana',12),bg = 'red', command = self.delete_order)
    self.update = Button(self.entry, text = 'Sipariş Güncelle', font =('Verdana',12), bg = 'yellow', command = self.update_order)
    
    #Canvas
    self.canvas = Canvas(self.m1st)
    self.canvas.pack(side = LEFT ,fill = BOTH, expand = 1)
    self.m2nd = Frame(self.canvas, width = 552,height = 500)
    
    #ScrollBars
    self.yscroll = Scrollbar(self.view)
    self.yscroll.pack(side = RIGHT , fill = Y)
    
    self.mscroll = Scrollbar(self.m1st, command = self.canvas.yview, orient = VERTICAL)
    self.mscroll.pack(side = RIGHT, fill = Y)
    
    #TreeView
    self.tree = ttk.Treeview(self.view,selectmode = 'browse',yscrollcommand=self.yscroll.set)
    self.tree['columns'] = ('Cno','oid','mno','Cname', 'Mname','Orderquantity','Current','Sdate','Edate')
    self.headings=['Müşteri No','Sipariş No','Makine No','Müşteri Adı','Makine Adı','Sipariş Adeti','Şu anki Durum','Başlangıç Tarihi','Teslim Tarihi']
    self.tree.column('#0', width= 0, stretch= NO)
    for i,j in zip(self.tree['columns'], self.headings):
        self.tree.column(i,anchor= CENTER,width= 120, minwidth= 120)
        self.tree.heading(i,text = j, anchor = CENTER)
        
    self.tree['displaycolumns'] = ('Cname', 'Mname','Orderquantity','Current','Sdate','Edate')
    
    #Configuration of Scrollbar for treeview
    self.yscroll.config(command= self.tree.yview)
    self.tree.bind('<Double-1>',self.selector)
    
    #Configuration of Canvas
    self.canvas.configure(yscrollcommand = self.mscroll.set)
    self.canvas.bind('<Configure>', lambda e: self.canvas.configure(scrollregion = self.canvas.bbox('all')))
    self.canvas.create_window((0,0), window = self.m2nd, anchor = 'nw')
    
    #Placement of Frames and TreeView
    self.entry.place_configure(x=15,y=95,height= 143, width=445)
    self.view.place_configure(width=900,height =700,x=610,y=90)
    self.m1st.place_configure(width=552, height = 500,x = 15, y = 265)
    self.tree.place(x=0, y=0 , height = 700 , width = 882)
    
    #Placement of Labels
    self.nlabel.place(x=15 , y=15)
    self.mlabel.place(x= 15,y=70)
    
    #Placement of ComboBoxes
    self.centry.place(x = 162 , y = 15)
    
    #Placement of EntryBoxes
    self.mqentry.place(x = 320 , y = 70)
    self.orderid.pack_forget()

    #Placement of Buttons
    self.backbutton.place(x=650,y= 0)
    self.add.place(x=15,y=100)
    self.update.place(x=150,y=100)
    self.delete.place(x=325,y=100)
    self.window.mainloop()

def goback(self):
    for i in self.window.winfo_children():
        i.destroy()
    self.window.state('normal')
    self.root = openingwindow(self.window)

def add_order(self):
    if (self.mqentry.get() == '') or (self.centry.get() not in cname):
        messagebox.showwarning('','Müşteri Adı ya da Makine Adet kısmı hatalı')
    else:
        try:
            quantity = int(self.mqentry.get())
            if len(self.m2nd.winfo_children()) != 0:
                for i in self.m2nd.winfo_children():
                    i.destroy()
            
            #Lists for storing widgets
            mnamelist = []
            mquantity = []
            dateinfo = []
            
            #Resizing Machine Entry Frame
            self.m1st.config(height=(quantity+1)*56.7, width= 500)
            print(self.m1st.winfo_height())
            
            #Labels for Machine Entry Frame
            machinenlabel = Label(self.m2nd,text = 'Makine Adı', font = ('Verdana',12))
            machineqlabel = Label(self.m2nd,text = 'Makine Adet', font = ('Verdana',12))
            datelabel = Label(self.m2nd,text = 'Teslim Tarihi', font = ('Verdana',12))
            machinenlabel.place(x = 31, y=0)
            machineqlabel.place(x = 170, y= 0)
            datelabel.place(x = 322, y = 0)
            
            yplace = 35
            for i in range(quantity):
                nolabel = Label(self.m2nd, text = str(i+1)+'.', font = ('Verdana',12))
                nolabel.place(x = 0, y = yplace)
                machines = ttk.Combobox(self.m2nd, width = 13, values = mname)
                machines.place(x = 31, y = yplace)
                mnamelist.append(machines)
                machineq = Entry(self.m2nd, width = 10, font = ('Verdana',12))
                machineq.place(x= 170, y = yplace)
                mquantity.append(machineq)
                date = Entry(self.m2nd, width = 10, font = ('Verdana',12))
                date.place(x = 322, y= yplace)
                dateinfo.append(date)
                yplace += 55
            
            
            cur.execute('''SELECT MAX(OrderID) FROM Orders''')
            MaxNo = cur.fetchone()[0]
                
            
        except ValueError:
            messagebox.showwarning('','Makine adet kısmı tam sayı olmalı')
    
    
    
    
    pass

def delete_order(self):
    if self.orderid.get() == '':
        messagebox.showwarning('','Önce listeden sipariş seçiniz')
    else:
        cur.execute('''DELETE FROM Orders WHERE OrderID = ?''',(self.orderid.get(),))
        cur.execute('''DELETE FROM has WHERE Orderid = ?''',(self.orderid.get(),))
        cur.execute('''DELETE FROM Includes WHERE OID = ?''',(self.orderid.get(),))
        con.commit()
        selected = self.tree.focus()
        self.tree.delete(selected)


def update_order(self):
    pass

def cleaner(self):
    self.mqentry.delete(0,END)
    self.centry.set('')
    self.orderid.delete(0,END)

def selector(self,e):
    self.cleaner
    selected = self.tree.focus()
    values = self.tree.item(selected,'values')
    pass

There are two issues:有两个问题:

  • you need to update the scrollregion of self.canvas whenever self.m2nd is resized, not self.canvas .每当self.m2nd调整大小时,您都需要更新scrollregionself.canvas区域,而不是self.canvas That is bind <Configure> on self.m2nd instead of self.canvas .这是在self.m2nd而不是self.canvas上绑定<Configure>

  • you need to use grid() instead of place() when inserting those comboboxes and entries into self.m2nd , otherwise the size of self.m2nd will not be changed when adding those widgets into it.在将这些组合框和条目插入self.m2nd时,您需要使用grid()而不是place() ,否则在将这些小部件添加到其中时, self.m2nd的大小不会改变。

...
class owindow:
    def __init__(self, window):
        ...
        #Configuration of Canvas
        self.canvas.configure(yscrollcommand = self.mscroll.set)
        #-- bind on self.m2nd instead of self.canvas
        self.m2nd.bind('<Configure>', lambda e: self.canvas.configure(scrollregion = self.canvas.bbox('all')))
        self.canvas.create_window((0,0), window = self.m2nd, anchor = 'nw')
        ...

    ...

    def add_order(self):
        if (self.mqentry.get() == '') or (self.centry.get() not in cname):
            messagebox.showwarning('','Müşteri Adı ya da Makine Adet kısmı hatalı')
        else:
            try:
                ...
                #Labels for Machine Entry Frame
                machinenlabel = Label(self.m2nd,text = 'Makine Adı', font = ('Verdana',12))
                machineqlabel = Label(self.m2nd,text = 'Makine Adet', font = ('Verdana',12))
                datelabel = Label(self.m2nd,text = 'Teslim Tarihi', font = ('Verdana',12))
                '''
                machinenlabel.place(x = 31, y=0)
                machineqlabel.place(x = 170, y= 0)
                datelabel.place(x = 322, y = 0)
                '''
                machinenlabel.grid(row=0, column=1)
                machineqlabel.grid(row=0, column=2)
                datelabel.grid(row=0, column=3)

                #yplace = 35
                for i in range(quantity):
                    nolabel = Label(self.m2nd, text = str(i+1)+'.', font = ('Verdana',12))
                    #nolabel.place(x = 0, y = yplace)
                    nolabel.grid(row=i+1, column=0)
                    machines = ttk.Combobox(self.m2nd, width = 13, values = mname)
                    #machines.place(x = 31, y = yplace)
                    machines.grid(row=i+1, column=1)
                    mnamelist.append(machines)
                    machineq = Entry(self.m2nd, width = 10, font = ('Verdana',12))
                    #machineq.place(x= 170, y = yplace)
                    machineq.grid(row=i+1, column=2)
                    mquantity.append(machineq)
                    date = Entry(self.m2nd, width = 10, font = ('Verdana',12))
                    #date.place(x = 322, y= yplace)
                    date.grid(row=i+1, column=3)
                    dateinfo.append(date)
                    #yplace += 55
                cur.execute('''SELECT MAX(OrderID) FROM Orders''')
                MaxNo = cur.fetchone()[0]

            except ValueError:
                messagebox.showwarning('','Makine adet kısmı tam sayı olmalı')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM