0
votes

So I am trying to allow the user to upload an image file for a flag, and then pack it into the frame. However I get the following error message cannot use geometry manager pack inside . which already has slaves managed by grid I do not understand why this is happening because I have tons of other widgets using .pack in the same frame. I have tried using .pack on the frames but I get this other problem where my program just crashes. Here is the code.

# Imports
from tkinter import * # Tkinter is a GUI toolkit used for Python. This toolkit allows me to create the window and many of the UI options
import Pmw # Pmw stands for 'Python mega widgets'. I imported this primarily for tooltips so the user knows what everything means
from PIL import ImageTk,Image
from tkinter import filedialog

# Windows
root = Tk() # Root is the main window where I will be putting everything
root.title('Nation Creator') # The title is the text at the top of every window
root.state('zoomed') # This makes the window go full screen
#Fonts
titlefont = ("Aldrich", 48)
subtitlefont = ("Aldrich", 22)
font = ("Times New Roman", 14)

#lists
hosl = ["Elected Monarch", "Hereditary Monarch", "Elected President", "Oligarchy"] #These are the four types of heads of state.
# Functions

#This is so that the user can scroll thru the next frame.
def nextframe(frame):
    frame.tkraise()

# Making the frames
def frameMaker():
    frameName = Frame(root)
    frameName.grid(row=0,column=0,sticky='nsew')
    return frameName

# This is what makes my Titles
def titleMaker(titleText, descriptionText, frame):
    title = Label(frame, text = titleText)
    title.configure(font=(titlefont))
    title.pack()
    description = Label(frame, text = descriptionText)
    description.configure(font=(subtitlefont))
    description.pack()
    return title, description

# Here I am making my spaces. It gives a better asthetic look.
def spaceMaker(spaceName, frame):
    spaceName = Label(frame, text="")
    spaceName.pack()
    return spaceName

#This opens a file browser so you can select your flag
def flagOpener():
        global flagIMG
        global flagLabel
        flagPath = filedialog.askopenfilename(initialdir="/", title="Select an Image File", filetypes=(("Png Files", "*.png"), ("Jpeg Files", "*.jpg; *.jpeg"), ("All Files", "*.*")))
        flagIMG = ImageTk.PhotoImage(Image.open(flagPath))
        flagLabel = Label(image=flagIMG)
        flagLabel.pack()
        return flagIMG, flagLabel
#######################################################
#######################FRAMES##########################
#######################################################

# These two commands allow the different frames to work
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
# There will be 4 different frames.
titleframe = frameMaker()
politicalframe = frameMaker()
econframe = frameMaker()
miscframe = frameMaker()

for frame in (titleframe, politicalframe, econframe, miscframe):
    frame.grid(row=0,column=0,sticky='nsew')

nextframe(titleframe)
# Section Headings
# These will be the headings for the sections.
# There will be 4 sections; Title Section, Political, Economic, and Micellanious 

titleMaker("Nation Creator","Version 1.0",titleframe)
startbutton = Button(titleframe, text = "Begin", command=lambda:nextframe(politicalframe))
startbutton.pack()

#Political Section

titleMaker("Political Section","This is where your nation's name, flag, territory, \nand political and personal freedoms are entered.",politicalframe)

spaceMaker("pOne", politicalframe)

nationNamePrompt = Label(politicalframe, text = "Enter your nation's name:")
nationNamePrompt.configure(font = (font))
nationNamePrompt.pack()
nationNameInput = Entry(politicalframe)
nationNameInput.configure(font=(font))
nationNameInput.pack()

spaceMaker("pTwo", politicalframe)

flagSelect = Button(politicalframe, text = "Select Flag", command=flagOpener)
flagSelect.pack()


spaceMaker("pThree", politicalframe)

territoryPrompt = Label(politicalframe, text = "List Your Territory")
territoryPrompt.configure(font=(font))
territoryPrompt.pack()
territoryButton = Entry(politicalframe)
territoryButton.configure(font=(font))
territoryButton.pack()

spaceMaker("pFour", politicalframe)

headOfStateVar = StringVar(politicalframe)
headOfStateVar.set("Select Head of State")
headOfStateType = OptionMenu(politicalframe, headOfStateVar, *hosl)
headOfStateType.pack()

spaceMaker("pFive", politicalframe)

headOfStateNamePrompt = Label(politicalframe, text = "What is the name of your head of state?")
headOfStateNamePrompt.configure(font=(font))
headOfStateNamePrompt.pack()
headOfStateName = Entry(politicalframe)
headOfStateName.configure(font=(font))
headOfStateName.pack()

spaceMaker("pSix", politicalframe)

polcontbutton = Button(politicalframe, text = "Next", command=lambda:nextframe(econframe))
polcontbutton.pack()
#Economic Section 
titleMaker("Economic Section","This is where your economic freedoms and control are entered.",econframe)

econbackbutton = Button(econframe, text = "Back", command=lambda:nextframe(politicalframe))
econbackbutton.pack()

# Making sure the window will stay up
root.mainloop()
1
You know that in the flagOpener() function you defined the tkinter.Label without a master? - TheLizzard

1 Answers

2
votes

In your flagOpener() function:

def flagOpener():
        global flagIMG
        global flagLabel
        flagPath = filedialog.askopenfilename(initialdir="/", title="Select an Image File", filetypes=(("Png Files", "*.png"), ("Jpeg Files", "*.jpg; *.jpeg"), ("All Files", "*.*")))
        flagIMG = ImageTk.PhotoImage(Image.open(flagPath))
        flagLabel = Label(image=flagIMG)
        flagLabel.pack()
        return flagIMG, flagLabel

You didn't specify a parent for the flagLabel. Tkinter used the default option for master which is root but you used the grid manager for root here:

def frameMaker():
    frameName = Frame(root)
    frameName.grid(row=0,column=0,sticky='nsew')
    return frameName

and here:

titleframe = frameMaker()
politicalframe = frameMaker()
econframe = frameMaker()
miscframe = frameMaker()

for frame in (titleframe, politicalframe, econframe, miscframe):
    frame.grid(row=0,column=0,sticky='nsew')

Tkinter doesn't allow you to use different managers for the same frame/window. Also why do you call the .grid method twice for: (titleframe, politicalframe, econframe, miscframe)? Once in the frameMaker function then again the the for loop. You shouldn't do that.