英文:
Why am I not allowed to use grid?
问题
我正在编写一个名为eTasks
的应用程序。以下是用于创建新任务窗口的代码:
import tkinter as tk
from tkinter import ttk
from tkinter import *
# ... (省略了其他导入语句)
todohome = tk.Tk()
# 这是新任务窗口的创建部分,包括任务名称、分组、优先级、日期和时间等部分的定义
# ... (省略了创建任务窗口的部分代码)
def create_task():
# 这是创建任务的函数,包括将任务信息保存到数据库的部分
# ... (省略了保存任务信息的代码)
# 这是保存任务按钮的定义
create_task_btn = tk.Button(newtaskwin, command=create_task, width=10, text='Save').grid(row=9, column=0)
当运行此代码时,在shell中出现以下错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Users\ellio_6\Documents\etasks-main\src\Home.py", line 77, in newtaskwin
TaskNameLbl = tk.Label(tasknameframe, text='Task Name:').grid(row=1, column=0, pady=5)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\tkinter\__init__.py", line 2549, in grid_configure
self.tk.call(
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
这个错误是因为你尝试在同一个父窗口中同时使用grid
和pack
几何管理器。你的代码在tasknameframe
上使用了grid
,但还可能在其他地方使用了pack
。确保你没有在同一个父窗口中混合使用这两个几何管理器。
如果你需要使用grid
,请确保在同一个父窗口中只使用grid
或pack
,而不是两者混合使用。
英文:
I am coding an app called eTasks
. Below is the code for the window that shows when you want to create a new task:
import tkinter as tk
from tkinter import ttk
from tkinter import*
from time import strftime
from datetime import date
from tkinter import messagebox
import TaskCls as ct
from TaskCls import task
import shelve
#import python_weather
import asyncio
import os
import quickbar as qb
from task_obj import nTask
import task_obj as nt
import pickle
import pyodbc
import pandas as pd
todohome=tk.Tk()
def newtaskwin():
#window is shown when create task button is pressed.
newtaskwin=Toplevel(todohome)
newtaskwin.geometry()
tasknameframe=tk.Frame(newtaskwin, relief='groove', highlightbackground='black', highlightthickness=1, width=250).grid()
#task name box
TaskNameLbl=tk.Label(tasknameframe, text='Task Name:').grid(row=1, column=0, pady=5)
TaskNameEntry=ttk.Entry(tasknameframe, width=100)
TaskNameEntry.grid(row=1, column=1, pady=5)
#Group to put the task into
GroupNameLbl=tk.Label(newtaskwin, text='Group:').grid(row=2, column=0, pady=5)
GroupNameEntry=ttk.Entry(newtaskwin, width=100)
GroupNameEntry.grid(row=2, column=1, pady=5)
#importance of task
PriorityLbl=tk.Label(newtaskwin, text='Priority:').grid(row=3, column=0, pady=5)
PriorityEntry=ttk.Combobox(newtaskwin, state="readonly", values=['High', 'Medium', 'Low'], width=97)
PriorityEntry.grid(row=3, column=1, pady=5)
#date the task will start
start_date_lbl=tk.Label(newtaskwin, text='Start Date:').grid(row=4, column=0, pady=5)
start_date_entry=ttk.Entry(newtaskwin, width=100)
start_date_entry.grid(row=4, column=1, pady=5)
#date the task will end
end_date_lbl=tk.Label(newtaskwin, text='End Date:').grid(row=5, column=0, pady=5)
end_date_entry=ttk.Entry(newtaskwin, width=100)
end_date_entry.grid(row=5, column=1, pady=5)
#time you will start the task
start_time_lbl=tk.Label(newtaskwin, text='Start Time:').grid(row=6, column=0, pady=5)
start_time_entry=ttk.Entry(newtaskwin, width=100)
start_time_entry.grid(row=6, column=1, pady=5)
#time you will finish the task
end_time_lbl=tk.Label(newtaskwin, text='End Time:').grid(row=7, column=0, pady=5)
end_time_entry=ttk.Entry(newtaskwin, width=100)
end_time_entry.grid(row=7, column=1, pady=5)
slaves = newtaskwin.grid_slaves()
print (f'grid slaves:{slaves}')
def create_task():
tName = TaskNameEntry.get()
gName=GroupNameEntry.get()
pName=PriorityEntry.get()
sDate=start_date_entry.get()
eDate=end_date_entry.get()
sTime=start_time_entry.get()
eTime=end_time_entry.get()
conn=pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\ellio_6\Documents\etasks-main\src\tasksdb.accdb')
cursor=conn.cursor()
sql="insert into TaskDB values ('{tName}', '{pName}', '{gName}', '{sDate}', '{eDate}', '{sTime}', '{eTime}')".format(
tName='Task Name', pName='High', gName = 'Group Name', sDate = '14/03/23', eDate = '15/03/23', sTime = '17:48', eTime = '18:48')
try:
cursor.execute(sql)
cursor.commit()
new_tsk=nt.nTask(tName, sDate, eDate, mainfrme)
new_tsk.pack(side=tk.TOP)
messagebox.showinfo('eTasks', 'Task saved')
except pyodbc.IntegrityError:
messagebox.showinfo('eTasks', 'You already have a task with that name. Please change it.')
#button to save the task to the db
create_task_btn=tk.Button(newtaskwin, command=create_task, width=10, text='Save').grid(row=9, column=0)
When you run the code, this error shows in shell:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Users\ellio_6\Documents\etasks-main\src\Home.py", line 77, in newtaskwin
TaskNameLbl=tk.Label(tasknameframe, text='Task Name:').grid(row=1, column=0, pady=5)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\tkinter\__init__.py", line 2549, in grid_configure
self.tk.call(
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
I know that you can't use more than one grid manager in a parent window, but I'm not doing that here.
I tried to get a list of all the packed and grided widgets; that wasn't much help. I've also tried to run the code by itself in a different file, and that didn't work either.
Using Python 3
.
答案1
得分: 1
tasknameframe 的几何管理器应该放在新的一行,所以不是:
tasknameframe=tk.Frame(newtaskwin, relief='groove', highlightbackground='black', highlightthickness=1, width=250).grid()
而应该是:
tasknameframe=tk.Frame(newtaskwin, relief='groove',
highlightbackground='black', highlightthickness=1, width=250)
tasknameframe.grid()
感谢所有在评论中提到这一点的人!
英文:
The geometry manager for tasknameframe should be on a new line, so instead of
> tasknameframe=tk.Frame(newtaskwin, relief='groove', highlightbackground='black', highlightthickness=1, width=250).grid()
It should be
tasknameframe=tk.Frame(newtaskwin, relief='groove',
highlightbackground='black', highlightthickness=1, width=250)
tasknameframe.grid()
Thank you all that posted this in the comments!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论