错误"cannot use geometry manager pack inside . which already has slaves managed by grid"



所以我试图允许用户上传一个标志的图像文件,然后将其打包到框架中。然而,我得到以下错误信息cannot use geometry manager pack inside . which already has slaves managed by grid我不明白为什么会发生这种情况,因为我有吨其他小部件使用.pack在同一框架。我试过在帧上使用.pack,但我得到了这个问题,我的程序只是崩溃。代码如下:

# 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()

flagOpener()函数中:

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

您没有为flagLabel指定父级。Tkinter使用默认选项root作为master,而你使用grid作为root管理器:

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

:

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

Tkinter不允许对同一个框架/窗口使用不同的管理器。另外,为什么要调用.grid方法两次:(titleframe, politicalframe, econframe, miscframe)?在frameMaker函数中,然后再次执行for循环。你不应该那样做。

相关内容

  • 没有找到相关文章

最新更新