0

Using tkinter in python 3.4. I'm struggling to place a list box without it automatically moving all other elements. The example code is using the grid placement. I've also attempted to use .pack() and .place(). I've tried to embed it into several frames, I've tried rowspan and columnspan.

The listbox1 is currently commented, this shows how the window should look, once this line is uncommented the positioning of the elements change. Id like to have the list box in the position under the "clear answers" button without effecting the other elements.

import sys
import time
import tkinter
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog

def CreateWindow():
    #Create the window and the menu bar
    MainWindow = Tk() 
    MainWindow.resizable(width=False, height=False) 
    MainWindow.geometry("450x260+150+150") 

    #Create the frame
    TopFrame = Frame(MainWindow).grid(row=0,column=0)

    #Top Labels
    a = Label(TopFrame,text="aaaa aaaaa aaa aaaa \naa aaaaaaa aaaaa:").grid(row=0,column=0)
    b = Label(TopFrame, text="aaaa aaaaa aaa aaaa \naa aaaaaaa aaaa?:").grid(row=0,column=1)
    c = Label(TopFrame, text="aaaa aa aaaaaaaa aaaaaaa:").grid(row=0,column=2)

    #Left Radio Buttons
    d = Radiobutton(TopFrame, text="aaaaa").grid(row=1,column=0,sticky=W)
    e = Radiobutton(TopFrame, text="aaaaa").grid(row=2,column=0,sticky=W)
    f = Radiobutton(TopFrame, text="aaaaa").grid(row=3,column=0,sticky=W)
    g = Radiobutton(TopFrame, text="aaaaa").grid(row=4,column=0,sticky=W)

    #Right Radio Buttons
    h = Radiobutton(TopFrame, text="aaaaa").grid(row=1,column=1,sticky=W)
    i = Radiobutton(TopFrame, text="aaaaa").grid(row=2,column=1,sticky=W)
    j = Radiobutton(TopFrame, text="aaaaa").grid(row=3,column=1,sticky=W)
    k = Radiobutton(TopFrame, text="aaaaa").grid(row=4,column=1,sticky=W)

    #Under left radio buttons
    l = Label(TopFrame,text="aaaaaa aa aaaaaaaa aaaaaa aaaaaa").grid(row=5,column=0,columnspan=2)
    m = Entry(TopFrame).grid(row=6,column=0,columnspan=2)
    n = Button(TopFrame,text="aaaaaaa").grid(row=7,column=0,columnspan=2)
    o = Label(TopFrame,text="aaaaa aaaaa aa").grid(row=8,column=0,columnspan=2)

    #clear button and list box
    ClearButton = Button(TopFrame,text="Clear answers").grid(row=1,column=2)

    #-----List box should be under the convert button and should make other elements move-----
    #ListBox1 = Listbox(TopFrame).grid(row=1,column=2)

    MainWindow.mainloop()

CreateWindow()

Is there anyway to position an element that doesn't effect elements around it, set an element to be on a higher layer, make it invisible to other elements or just solve this problem? Any help is massively appreciated!

3
  • Just by the way here, but your references a, b, c, ListBox1, etc. each point to None, which is the value returned by grid(). To avoid this, assign a widget in one statement, and then grid() it in a separate statement. Commented Apr 26, 2015 at 21:48
  • Okay, that makes sense, I typically do but I tried to simplify the code to post it here, thanks! Commented Apr 26, 2015 at 21:51
  • As a general rule of thumb, you shouldn't use absolute placement. Almost every arrangement you can think of can be done with grid and/or pack, and your GUI will be more resilient in the face of resizing or different sized fonts or resolutions. Commented Apr 26, 2015 at 22:54

1 Answer 1

1

You're looking for the rowspan argument.

ListBox1 = Listbox(TopFrame)
ListBox1.grid(row=2,column=2, rowspan=7)

It should also be in row=2, because row=1 in that column is holding ClearButton already.

Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly. I'd tried rowspan but not setting the value to 7, that must have been the issue. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.