【问题标题】:Only using grid Tkinter but still getting TclError仅使用网格 Tkinter 但仍然收到 TclError
【发布时间】: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


【解决方案1】:

这是您在这里遇到的一个非常奇怪的错误,我只是使用 canvas 和 pack 方法重写了您的代码,而不是网格、行和列。只要确保您使用的是 Python3x 就不会出现这些奇怪的错误,希望这会有所帮助,您可以在底部使用 x 和 y 值,并且可以在我们设置时弄乱顶部的高度和宽度值画布。编码愉快!

from tkinter import *

global path, source, recoding  # Global Values so that save_info can get the values
# Creates window
root = Tk()
root.title("File Information")

# Canvas Creates the base layer for the window, so instead of l = Label(text="Test").grid(row=2, column=3)
# We would now do l = Label(text="Test") 
# canvas.create_window(20, 30, anchor="nw", window=l)
canvas = Canvas(width=400, height=300)
canvas.pack(fill="both", expand=True)
# Canvas.pack() just finishes creating the canvas.

# This program opens the file with information and adds the new information to it.
def save_info():
    global path, source, recoding
    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()
    root.withdraw()
    print(finalList)
    return finalList


sourceLabel = Label(
    text="Source Data File Location:",
    width=21,
    height=2)

pathLabel = Label(
    text="Complete File Path:",
    width=20,
    height=2)

recoding_label = Label(
    text="Is recoding of column \n names and/or values \n desired:",
    width=20,
    height=4)

source = StringVar(root)
source.set("Local")  # default value
sourceOption = OptionMenu(root, source, "Local", "Google Drive", "One Drive")

path = Entry(fg="black", bg="white", width=35)

recoding = StringVar(root)
recoding.set("Yes")  # default value
recodingOption = OptionMenu(root, recoding, "Yes", "No")

button = Button(
    text="Click to Save",
    width=10,
    height=1,
    bg="white",
    fg="black", command=save_info
)

# Since we are now using canvas, we must add all the elements using canvas.create_window, the first int is the x value, 2nd is the y
# Just leave anchor always as nw, and windows need to equal the variable of the widget they need to add
canvas.create_window(0, 50, anchor="nw", window=sourceLabel)
canvas.create_window(0, 90, anchor="nw", window=pathLabel)
canvas.create_window(0, 140, anchor="nw", window=recoding_label)

canvas.create_window(150, 50, anchor="nw", window=sourceOption)
canvas.create_window(150, 90, anchor="nw", window=path)
canvas.create_window(150, 140, anchor="nw", window=recodingOption)
canvas.create_window(150, 225, anchor="nw", window=button)
root.mainloop()

# I refactored some of the variables so they would be unique

【讨论】:

    猜你喜欢
    • 2015-01-30
    • 1970-01-01
    • 2015-08-09
    • 2015-03-01
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多