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
#2
Quote:but I cannot get the sizing and scrolling to cooperate.
Even after using tkinter for over two years, I always had trouble with sizing.
I finally made the move to wxpython where it's a snap!. As a simple demo of what I'm talking about, see: https://python-forum.io/Thread-Perfectly...n-template
In about 80 lines of code (if you eliminate comments), you get a three panel, completely resizable and dock-able window (click on image). Tkinter is in my opinion only suitable for the easiest of gui's. As soon as you start to expand, you find trouble, if not sooner.

I know this is not what you're looking for, but perhaps is worth considering.
old version that runs in python 2.

Please, This is important, note that I'm talking about wxpython phoenix for python 3, not the old version which ran in pyrhon 2
Reply
#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
#4
Larz60+,
wxPython is intriguing, tKinter has been difficult to work with so far. I might go that route. "wxFormBuilder" makes it sound even better.

Wuf's solution of using a canvas worked really well and did solve the problem.

Thanks to both of you for helping me out. I think I will go the canvas direction for now and if it gives me more trouble, I think I'll throw it out in favor of wxPython. Again, thanks to both of you.
Reply
#5
I'm not sure that wxformbuilder is up to date, check it out first.
If you want to get a real taste for wxpython try the following (you can delete afterwards if you decide it's not for you)
Following for windows, Linux will be the same for some distros, but not all
  • install wxpython phoenix with pip (you must be running python 3 by default or you will get the wrong version of wxpython)
    pip install wxpython
  • In browser, navigate to: https://github.com/wxWidgets/Phoenix
  • Download zip (or from command line, if you have git installed:
    git clone https://github.com/wxWidgets/Phoenix
  • Extract the zip of tar file in a preselected directory
  • Navigate to Phoenix-master\demo
  • From command line, run
    python demo.py
  • You'll get a sync error which can be ignored.
Look around in the demo and exmaine the source code needed to create various widgets.
I think you will be surpeised and delighted by what you find.
Reply
#6
Very helpful Larz60+. It did not take long for me to run into more tKinter troubles so I installed wxpython. Very slick. And the demo is awesome!
Reply
#7
I just took a look at wxFormBuilder, and see that the last update was just 13 days ago.
So looks like it's worth investigating.
Glad your liking wxpython.
Reply


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