【发布时间】:2021-05-04 17:03:52
【问题描述】:
所以我刚开始使用 Google Colab,但一直收到此错误:
TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
我正在尝试制作一个 GUI 窗口来接收用户的信息并保存它。
我在网上阅读的所有内容都表明问题在于我使用的是pack() 和grid(),但我只使用了grid()。当我第一次尝试放置标签时,错误就开始了 (sourceLabel)。
我很困惑,任何帮助都会很棒。
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
from tkinter import *
except ImportError:
import Tkinter as tk
from Tkinter import *
#creates window
window = tk.Tk()
window.title("File Information")
window.rowconfigure([0,1], minsize=30)
window.columnconfigure([0, 1, 2, 3], minsize=30)
#this program opens the file with information and adds the new information to it
def saveInfo():
value = path.get()
loc = source.get()
recode = recoding.get()
#change name of file and will this make them see everything
#f = open("./info.txt", "a+")
#f.write("Source Data File Location: " + loc + ", Complete File Path: " + value + ", Is recoding of column names and/or values desired?: " + recode)
#f.flush()
#f.seek(0)
#content = f.read()
#print (content)
finalList = [value,loc,recode]
#f.close()
window.withdraw()
print (finalList)
return finalList
#creates a text label, fg=foreground and bg=background, theyre the locations of colors, width and height are measured by text units which are separate horizonatal and vertical
sourceLabel = tk.Label(
text="Source Data File Location:",
width = 21,
height=2)
#adds text to window
sourceLabel.grid(row=0,column=0)
#creates second label
pathLabel = tk.Label(
text="Complete File Path:",
width = 20,
height=2)
#adds text to window
pathLabel.grid(row=1,column=0)
#creates third label
sourceLabel = tk.Label(
text="Is recoding of column \n names and/or values \n desired:",
width = 20,
height=4)
#adds text to window
sourceLabel.grid(row=2,column=0)
#create dropdown for sources
source = StringVar(window)
source.set("Local") # default value
sourceOption = OptionMenu(window, source, "Local", "Google Drive", "One Drive")
sourceOption.grid(row=0,column=1,sticky="ew")
#adds path entry
path = tk.Entry(fg="black", bg="white", width=35)
#adds path to window
path.grid(row=1,column=1,sticky="ew")
#create dropdown for recoding
recoding = StringVar(window)
recoding.set("Yes") # default value
recodingOption = OptionMenu(window, recoding, "Yes", "No")
recodingOption.grid(row=2,column=1,sticky="new")
#creates the click to save button
button = tk.Button(
text="Click to Save",
width=10,
height=1,
bg="white",
fg="black", command=saveInfo
)
#adds Button to window
button.grid(row=4,column=1,sticky="w")
#runs window
window.mainloop()
window.destroy()
【问题讨论】:
-
我无法使用您问题中的代码重现几何管理器问题。在最后一行的
window.destroy()调用上关闭应用程序窗口时出现不同的错误(因为此时应用程序窗口已被破坏。 -
你做了什么导致错误出现?最后你也不需要
window.destroy()。 -
您发布的代码无法给出您所说的错误。请将此代码复制并粘贴到一个新文件中,运行它,然后向我们显示错误的完整堆栈跟踪。
-
这些小部件是否要在根窗口以外的窗口中创建?
-
在创建小部件时尝试指定父级 (
window)。
标签: python tkinter geometry label