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
#3
Hi kellykimble

The purpose of a listbox widget is to display a set of lines of text not Widgets. But you would like to display a pair of Labels per line representing colors. For this you can't use the listbox-widget. One way is to put a frame containing all color labels in a canvas. Then it is possible to scroll the frame in the canvas. Look at the following skript:
from tkinter import *

def canvas_update(event):
    canvas = event.widget
    canvas.config(scrollregion=canvas.bbox('all'))
     
def main () :
 
    window = Tk()
    window.title("Colors")
 
    #screen_width = window.winfo_screenwidth()
    #screen_height = window.winfo_screenheight()
    #window.geometry(str(screen_width)+"x"+str(screen_height)) 
     
    main_frame = Frame(window, relief='groove', bd=2) 
    main_frame.pack(expand=True, anchor='nw', padx=10, pady=10)
    
    canvas = Canvas(main_frame, width=200, height=400, highlightthickness=0)
    canvas.pack(side='left', expand=True)
    canvas.bind('<Configure>', canvas_update)

    scrollbar = Scrollbar(main_frame, orient="vertical")
    scrollbar.pack(side='left', fill='y')
    
    canvas.config(yscrollcommand=scrollbar.set)
    scrollbar.config(command=canvas.yview)

    color_frame = Frame(canvas)
    canvas.create_window(0, 0, anchor='nw', window=color_frame)

    #myframe.grid_propagate(0) 
    # Simulate the color ranges:
    r1 = 250
    g1 = 0
    b1 = 0
    r2 = 0
    g2 = 0
    b2 = 0
 
    for ii in range(25):
        r1 -= 10
        g1 += 5
 
        mycolor = '#%02x%02x%02x' % (r1, g1, b1)
        mycolor2 = '#%02x%02x%02x' % (b2, g2, r2)
        
        list_frame = Frame(color_frame, relief='sunken')
        list_frame.pack(fill='x')
 
        Label(list_frame, text=str(ii), bg=mycolor, fg='black', width=12,
            height=4).pack(side='left')
        Label(list_frame, text=str(ii), bg=mycolor2, fg='black', width=12,
            height=4).pack(side='left')
        
    window.mainloop()

if __name__ == '__main__':
    main()
Greetings wuf Wink
Reply


Messages In This Thread
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,453 Mar-28-2023, 12:58 PM
Last Post: Axel_Erfurt
  [Tkinter] Scrollable buttons with an add/delete button Clich3 5 3,662 Jun-16-2022, 07:19 PM
Last Post: rob101
  [Tkinter] How to create scrollable frame mandiatutti 7 4,742 Aug-07-2021, 03:34 PM
Last Post: deanhystad
Question [Tkinter] Scrollable Treeview: change behavior of Prior/Next keys? RockDoctor 2 3,336 Apr-10-2021, 05:40 PM
Last Post: RockDoctor
  Scrollable big image in a window (TKinter) Prospekteur 3 4,638 Sep-14-2020, 03:06 AM
Last Post: Larz60+
  [Tkinter] Fixate graphs on scrollable canvas janema 6 5,582 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