Python Forum
tkinter questions--- part 1
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter questions--- part 1
#1
Why is it that when I put
from tkinter import *
tk=Tk()
canvas=Canvas(tk, width=500,height=550)
tk.title("stuff")
A small screen appears not following the height and width, but when I put
from tkinter import *
tk=Tk()
canvas=Canvas(tk, width=500,height=550)
tk.title("stuff")
canvas.pack()
the screen matches the width and height? What does canvas.pack() mean??? It is strange because of the word pack?
Reply
#2
pack is one of three geometry managers, there's also place and grid.
it 'packs' the widgets into a container, and is necessary to render the widgets.
You need to read up on how this works, it's an elementary part of GUI programming.
I would suggest reading: http://effbot.org/tkinterbook/wm.htm, and the next three pages.
Reply
#3
from tkinter import *
tk=Tk()
canvas=Canvas(tk, width=500,height=550)
tk.title("stuff")
canvas.pack()
Pack is literally going to pack everything where it can fit.
Try using "grid"

Here is an example for a login GUI
it doesn't do anything but is a good example of how grid works (at least I think it is)

from tkinter import *

root = Tk()
# Create Label
label_1 = Label(root, text="Name")
label_2 = Label(root, text="Password")
# Create Entry Field
entry_1 = Entry(root)
entry_2 = Entry(root)
# Placement of Label
label_1.grid(row=0, column=1, sticky=E)
label_2.grid(row=1, column=1, sticky=E)
#Placement of Entry Fields
entry_1.grid (row=0, column=2)
entry_2.grid (row=1, column=2)
#Check button
c = Checkbutton(root, text="Keep me signed in")
c.grid(columnspan=3)
root.mainloop()
Reply
#4
http://effbot.org/tkinterbook/wm.htm ---- I checked it out and it is throwing me off with all this technical stuff-- I am new to python and this article looks like it was made for people who are already well immersed into python. Also, grid also works, thank you for the substitute.

I am new to python and I am learning it through Chris Bradfield's Youtube Channel, "Kidscancode".
Reply
#5
You'll pick up the geometry as you code more. The main thing to remember now is that for any window, all widgets in that window have to use the same geometry. If one used grid, they all have to, if one uses pack, they all have to, can't mix two together or all sorts of crazy things start to happen.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter questions--- part 2 ironsheep 8 3,996 Dec-09-2018, 01:44 AM
Last Post: ironsheep

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020