1

enter image description here I am coding for my Shop, but it look like I can't make a good scrollbar. Does anyone could help me, I've searched for make scrollbar, but they just use .pack(). So its seem like I need to make another Frame, but I don't want use it. Here is my code.

from tkinter import *
import tkinter as tk
wd=tk.Tk()
wd.title("Sale manager")
#------------Frame-------------
#f1=Frame(wd)
#f1.pack()
#------------Label--------------
lb0 = Label(wd, text ="The FOAK Store", bg = "red", fg ="Black")
lb0.grid(row =0, column =0)
lb1 = Label(wd, text ="Chon loai giay:")
lb1.grid(row =1, column =0)
lb2 = Label(wd, text ="Ngay:")
lb2.grid(row =2, column =0)
lb3 = Label(wd, text ="Gia ban:")
lb3.grid(row =3, column =0)
lb4 = Label(wd, text ="Gia goc:")
lb4.grid(row =4, column =0)

#------------Entry-----------------
scrollbar = Scrollbar(wd)
scrollbar.grid(row=1, column=2)

listbox = Listbox(wd, height=5, width=30, yscrollcommand=scrollbar)
listbox.grid(row=1, column=1)
#------------Data giày-------------
Giay = [
"Nike Air Max Offwhite",
"Nike Vapor Max Offwhite",
"Nike Jordan 1 Offwhite",
"Yeezy 350 Sesame"
]
#-------------Import list box---------
for i in Giay:
   listbox.insert(END, i)

wd.mainloop()
Nouman
  • 6,947
  • 7
  • 32
  • 60
  • Widgets are by default centered in their cells. You can change this behaviour by using the `sticky` option, which will be `scrollbar.grid(row=1, column=2,sticky="ns")`. – Henry Yik Aug 19 '19 at 04:30
  • Your scrollbar is not properly connected to your listbox. Please read a tutorial about scrollbars (e.g. http://effbot.org/tkinterbook/scrollbar.htm). – j_4321 Aug 19 '19 at 11:11

1 Answers1

2

Welcome to stackoverflow.

As the comments have said

  • a) Use sticky to stretch the scrollbar to fill the row.
  • b) Link the yscrollcommand of the listbox to scrollbar.set
  • c) Link the scrollbar command to listbox.yview

The changes to your code are shown below. They fit between # -- Entry -- and # -- Data giày --.

There must be a few more items in the list to make it long enough to scroll as well.

   #------------Entry-----------------
   scrollbar = Scrollbar(wd)
   scrollbar.grid(row=1, column=2, sticky=tk.N+tk.S) # Scrollbar fills the height of row 1

   listbox = Listbox(wd, height=5, width=30, yscrollcommand=scrollbar.set)
   # yscrollcommand linked to scrollbar.set method, not scrollbar 
   listbox.grid(row=1, column=1)
   scrollbar['command']=listbox.yview # Bind scrollbar command to listbox.yview

   #------------Data giày-------------

On my windows machine the tk and ttk scrollbars look the same. You may want to explore using the ttk scrollbar, it may improve the appearance.

Tls Chris
  • 3,564
  • 1
  • 9
  • 24