1

I'm making a GUI using .grid() because it has lot of buttons. I've put those in an interactive Frame at the top of the screen (frameone) and I have frametwo at the bottom where I want to print out messages based on what buttons a user presses. It's a battleship game, for context. but when I make frametwo and put a listbox in it, the listbox is resized to fit the text inside. I don't want to have to resize the window every time more labels are entered. Here's a working example:

from tkinter import *
import tkinter as tk
window = Tk()
window.title("Window")
window.geometry('150x350')     #I wanted the demonstration to work for you.

def addline():                 #Adding sample text
    Label(listbox, text='this is text').grid(sticky=N+W)

frameone = Frame(window, height=10, width=10)
frameone.grid(sticky=N)        #The top where the button goes...

Label(frameone, bg='yellow', text='This is frameone\n\n\n').grid(row=0, column=0)
                               #The yellow here is where all the buttons go...
addtext = Button(frameone, text = 'Add line:', command=addline)
addtext.grid(column=0,row=1)   #Button to add text...

frametwo = Frame(window, bg='red', height=10, width=10)
frametwo.grid(sticky=W)        

listbox = Listbox(frametwo)
listbox.grid(sticky=W, pady=3, padx=3)         #This is the listbox that is wrongly resizing.

scrolltwo = Scrollbar(window, orient=HORIZONTAL)
scrolltwo.configure(command=listbox.yview)
scrolltwo.grid(sticky=S+E)     #I got lazy, I will put this on the side w/rowspan etc.

window.mainloop()

If this is a repeat question or somehow super obvious, I apologize. Also, sorry my GUI is hideous... I don't understand why this method isn't working. While looking for solutions, all I found were some very good explanations of .grid and how to make lists resize when they aren't. Everything helps, thanks.

McKay L
  • 25
  • 7

1 Answers1

1

You are treating the Listbox, listbox, like a Frame, when in fact it works differently. To add items to a Listbox, use it's insert function. So, to fix your problem, replace:

Label(listbox, text='this is text').grid(sticky=N+W)

With:

listbox.insert(END, 'this is text')

For more information on the tkinter Listbox widget, see the effbot documentation, here.

Artemis
  • 2,553
  • 7
  • 21
  • 36