sw123456
sw123456

Reputation: 3459

A solution to using both the .pack() and .grid() method in the same tkinter window?

So I have been playing around with widgets in Tkinter and due to an issue (which was solved on this forum) I realise that you can't have .pack() and .grid() methods to organise different widgets in the same window. The code below creates a canvas and I have added a scroll to this widget. Originally I had the canvas arranged using .grid() but I had to change this to .pack() when I added the scroll (as the scroll required .pack() to position it correctly).

from tkinter import *

x = 10
y = 10
a = 100
b = 100

def change_coord(event):
        global coord
        if event.keysym == 'Up':
            coord[1] -=1
            coord[3] -=3
        if event.keysym == 'Down':
            coord[1] +=1
            coord[3] +=3
        if event.keysym == 'Right':
            coord[0] +=1
            coord[2] +=3
        if event.keysym == 'Left':
            coord[0] -=1
            coord[2] -=3
        canvas1.coords(arc, *coord)


window = Tk()
window.geometry("500x500")


#canvas scroll bar
scrollbar = Scrollbar(window)
scrollbar.pack(side=RIGHT, fill=Y)

#canvas and drawing
canvas1=Canvas(window, yscrollcommand=scrollbar.set, height = 600, width = 600)
canvas1.pack()
coord = [x, y, a, b]
arc = canvas1.create_rectangle(*coord, outline="#fb0", fill="#fb0")

#canvas scrollbar continued
scrollbar.config(command=canvas1.yview)
canvas1.configure(scrollregion=(0,0,1000,1000))


#capturing keyboard inputs and assigning to function
window.bind_all('<Up>', change_coord)
window.bind_all('<Down>', change_coord)
window.bind_all('<Left>', change_coord)
window.bind_all('<Right>', change_coord)
window.mainloop()

My question therefore is: If I were to have more widgets in the window and as i am unable to use the .grid() method to arrange them due to the scrollable canvas, what is the best way to organise these other widgets as the .pack() method doesn't seem to be that easy to arrange widgets with? (grid seems far easier to place widgets exactly where you want)

E.G What is the best way to arrange these widget examples (inside the same window as the canvas above) instead of using the .grid() method?

#spinboxes and capturing value
v=IntVar()
spin = Spinbox(window, textvariable=v, from_=1, to = 10)
spin.grid(row=1, column = 0, sticky= W)

#checkboxes and capturing value
var1 = IntVar()
c1 = Checkbutton(window, text="Check!", variable=var1)
c1.grid(row=2, column = 0, sticky= W)
var2 = IntVar()
c2 = Checkbutton(window, text="Check Dis!", variable=var2)
c2.grid(row=3, column = 0, sticky= W)
var3 = IntVar()
c3 = Checkbutton(window, text="Check Dis Out!", variable=var3)
c3.grid(row=4, column = 0, sticky= W)

#message box like a label
message = Message(window, text="Test")
message.grid(row=5, column = 0, sticky= W)

#radiobuttons and caputring input
radiovar = IntVar()
rb = Radiobutton(window, text="One", variable=radiovar, value=1)
rb.grid(row=6, column = 0, sticky= W)
rb = Radiobutton(window, text="Two", variable=radiovar, value=2)
rb.grid(row=7, column = 0, sticky= W)

#radioboxes and caputring input
radiobutval = IntVar()
rb = Radiobutton(window, text="One", indicatoron=0, variable=radiobutval, value=10)
rb.grid(row=8, column = 0, sticky= W)
rb = Radiobutton(window, text="Two", indicatoron=0, variable=radiobutval, value=20)
rb.grid(row=9, column = 0, sticky= W)

Thanks

EDIT: Ok, so I have been reading around to try to get an answer. Would an effective method be to use frames? If I created, say, 2 frames, could some widgets be arranged in one frame using the .grid() method and other widgets (such as the canvas) be arranged in the other frame using the .pack() method? Is this a solution to using two different layout management methods in the same window?

Upvotes: 0

Views: 1149

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385910

If I created, say, 2 frames, could some widgets be arranged in one frame using the .grid() method and other widgets (such as the canvas) be arranged in the other frame using the .pack() method? Is this a solution to using two different layout management methods in the same window?

Yes, using grid in one frame and pack in another is a very common thing to do.

Upvotes: 2

Related Questions