Python Forum
[Tkinter] ListBox not contained or scrollable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] ListBox not contained or scrollable
#1
I am trying to use a Listbox to display and scroll through a saved list of "color ranges" but I cannot get the sizing and scrolling to cooperate.
This is my first attempt at using tKinter, and I am fairly new to Python as well so its been quite frustrating.
I expect it is something simple I am missing, but I have tried every solution I could Google.
Either the Scrollbar will not scroll the list, or the list grows out of its frame or grid box.

Any help would be appreciated.

from tkinter import *
import os
import sys

def main () :

    window = Tk() # create window
    window.title("Colors")

    screen_width = window.winfo_screenwidth()
    screen_height = window.winfo_screenheight()
    window.geometry(str(screen_width)+"x"+str(screen_height)) 

    window.resizable(0, 0) #Don't allow resizing in the x or y direction
    lbl2 = Label(window, text="HELLO", fg='black', font=("Helvetica", 16,"bold"))
    lbl2.pack()
    window.update()
    fontwidth = lbl2.winfo_width()
    fontheight = lbl2.winfo_height()
    frameWidth = screen_width * .15
    lbl2.destroy()

    myframe=Frame(window,width=frameWidth+20, height = fontheight * 12) 
    myframe.grid()
    myframe.grid_propagate(0) 

    scrollbar = Scrollbar(orient="vertical")
    listSelection = Listbox(myframe, width=int(frameWidth*.95), height=4)
    listSelection.grid(row=0, column=0,sticky=N+S+W)
    #listSelection.grid_propagate(0)
    listSelection.yscrollcommand = scrollbar.set
    scrollbar.command = listSelection.yview
    scrollbar.grid(row=0, column=1,sticky=N+S+E)
    scrollbar.grid_propagate(0)
    listSelection.yview_scroll(1,"units")
    listSelection.yview_scroll(1,"units")

    # Simulate the color ranges:
    r1 = 250
    g1 = 0
    b1 = 0
    r2 = 0
    g2 = 0
    b2 = 0

    for ii in range(0,25):
        r1 -= 10
        g1 += 5

        mycolor = '#%02x%02x%02x' % (r1, g1, b1)  # set your favourite rgb color
        mycolor2 = '#%02x%02x%02x' % (b2, g2, r2)  # set your favourite rgb color
        frameWidth2 = int(frameWidth * .25)
        f = Frame(listSelection,width=frameWidth, height = fontheight * 2, relief=SUNKEN)
        f.grid()
        f.grid_propagate(0) 
        a=Label(f, text="   " + str(ii) + "   ", bg=mycolor, fg='black',width=12)
        b=Label(f, text="   " + str(ii) + "   ", bg=mycolor2, fg='black',width=12,height = 4)
        a.grid(row = 0, column = 0, sticky=N+S)
        b.grid(row = 0, column = 1, sticky=N+S)

    window.mainloop()

if __name__ == '__main__':
    main()
Reply


Messages In This Thread
ListBox not contained or scrollable - by kellykimble - Apr-04-2018, 11:59 AM
RE: ListBox not contained or scrollable - by wuf - Apr-04-2018, 04:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] PyQt5 drawing on scrollable area HeinKurz 3 1,389 Mar-28-2023, 12:58 PM
Last Post: Axel_Erfurt
  [Tkinter] Scrollable buttons with an add/delete button Clich3 5 3,551 Jun-16-2022, 07:19 PM
Last Post: rob101
  [Tkinter] How to create scrollable frame mandiatutti 7 4,583 Aug-07-2021, 03:34 PM
Last Post: deanhystad
Question [Tkinter] Scrollable Treeview: change behavior of Prior/Next keys? RockDoctor 2 3,278 Apr-10-2021, 05:40 PM
Last Post: RockDoctor
  Scrollable big image in a window (TKinter) Prospekteur 3 4,561 Sep-14-2020, 03:06 AM
Last Post: Larz60+
  [Tkinter] Fixate graphs on scrollable canvas janema 6 5,479 Apr-12-2019, 03:57 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020