为什么我不能使用网格?

huangapple go评论237阅读模式
英文:

Why am I not allowed to use grid?

问题

我正在编写一个名为eTasks的应用程序。以下是用于创建新任务窗口的代码:

  1. import tkinter as tk
  2. from tkinter import ttk
  3. from tkinter import *
  4. # ... (省略了其他导入语句)
  5. todohome = tk.Tk()
  6. # 这是新任务窗口的创建部分,包括任务名称、分组、优先级、日期和时间等部分的定义
  7. # ... (省略了创建任务窗口的部分代码)
  8. def create_task():
  9. # 这是创建任务的函数,包括将任务信息保存到数据库的部分
  10. # ... (省略了保存任务信息的代码)
  11. # 这是保存任务按钮的定义
  12. create_task_btn = tk.Button(newtaskwin, command=create_task, width=10, text='Save').grid(row=9, column=0)

当运行此代码时,在shell中出现以下错误:

  1. Exception in Tkinter callback
  2. Traceback (most recent call last):
  3. File "C:\Program Files\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
  4. return self.func(*args)
  5. ^^^^^^^^^^^^^^^^
  6. File "C:\Users\ellio_6\Documents\etasks-main\src\Home.py", line 77, in newtaskwin
  7. TaskNameLbl = tk.Label(tasknameframe, text='Task Name:').grid(row=1, column=0, pady=5)
  8. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  9. File "C:\Program Files\Python311\Lib\tkinter\__init__.py", line 2549, in grid_configure
  10. self.tk.call(
  11. _tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack

这个错误是因为你尝试在同一个父窗口中同时使用gridpack几何管理器。你的代码在tasknameframe上使用了grid,但还可能在其他地方使用了pack。确保你没有在同一个父窗口中混合使用这两个几何管理器。

如果你需要使用grid,请确保在同一个父窗口中只使用gridpack,而不是两者混合使用。

英文:

I am coding an app called eTasks. Below is the code for the window that shows when you want to create a new task:

  1. import tkinter as tk
  2. from tkinter import ttk
  3. from tkinter import*
  4. from time import strftime
  5. from datetime import date
  6. from tkinter import messagebox
  7. import TaskCls as ct
  8. from TaskCls import task
  9. import shelve
  10. #import python_weather
  11. import asyncio
  12. import os
  13. import quickbar as qb
  14. from task_obj import nTask
  15. import task_obj as nt
  16. import pickle
  17. import pyodbc
  18. import pandas as pd
  19. todohome=tk.Tk()
  20. def newtaskwin():
  21. #window is shown when create task button is pressed.
  22. newtaskwin=Toplevel(todohome)
  23. newtaskwin.geometry()
  24. tasknameframe=tk.Frame(newtaskwin, relief='groove', highlightbackground='black', highlightthickness=1, width=250).grid()
  25. #task name box
  26. TaskNameLbl=tk.Label(tasknameframe, text='Task Name:').grid(row=1, column=0, pady=5)
  27. TaskNameEntry=ttk.Entry(tasknameframe, width=100)
  28. TaskNameEntry.grid(row=1, column=1, pady=5)
  29. #Group to put the task into
  30. GroupNameLbl=tk.Label(newtaskwin, text='Group:').grid(row=2, column=0, pady=5)
  31. GroupNameEntry=ttk.Entry(newtaskwin, width=100)
  32. GroupNameEntry.grid(row=2, column=1, pady=5)
  33. #importance of task
  34. PriorityLbl=tk.Label(newtaskwin, text='Priority:').grid(row=3, column=0, pady=5)
  35. PriorityEntry=ttk.Combobox(newtaskwin, state="readonly", values=['High', 'Medium', 'Low'], width=97)
  36. PriorityEntry.grid(row=3, column=1, pady=5)
  37. #date the task will start
  38. start_date_lbl=tk.Label(newtaskwin, text='Start Date:').grid(row=4, column=0, pady=5)
  39. start_date_entry=ttk.Entry(newtaskwin, width=100)
  40. start_date_entry.grid(row=4, column=1, pady=5)
  41. #date the task will end
  42. end_date_lbl=tk.Label(newtaskwin, text='End Date:').grid(row=5, column=0, pady=5)
  43. end_date_entry=ttk.Entry(newtaskwin, width=100)
  44. end_date_entry.grid(row=5, column=1, pady=5)
  45. #time you will start the task
  46. start_time_lbl=tk.Label(newtaskwin, text='Start Time:').grid(row=6, column=0, pady=5)
  47. start_time_entry=ttk.Entry(newtaskwin, width=100)
  48. start_time_entry.grid(row=6, column=1, pady=5)
  49. #time you will finish the task
  50. end_time_lbl=tk.Label(newtaskwin, text='End Time:').grid(row=7, column=0, pady=5)
  51. end_time_entry=ttk.Entry(newtaskwin, width=100)
  52. end_time_entry.grid(row=7, column=1, pady=5)
  53. slaves = newtaskwin.grid_slaves()
  54. print (f'grid slaves:{slaves}')
  55. def create_task():
  56. tName = TaskNameEntry.get()
  57. gName=GroupNameEntry.get()
  58. pName=PriorityEntry.get()
  59. sDate=start_date_entry.get()
  60. eDate=end_date_entry.get()
  61. sTime=start_time_entry.get()
  62. eTime=end_time_entry.get()
  63. conn=pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\ellio_6\Documents\etasks-main\src\tasksdb.accdb')
  64. cursor=conn.cursor()
  65. sql="insert into TaskDB values ('{tName}', '{pName}', '{gName}', '{sDate}', '{eDate}', '{sTime}', '{eTime}')".format(
  66. tName='Task Name', pName='High', gName = 'Group Name', sDate = '14/03/23', eDate = '15/03/23', sTime = '17:48', eTime = '18:48')
  67. try:
  68. cursor.execute(sql)
  69. cursor.commit()
  70. new_tsk=nt.nTask(tName, sDate, eDate, mainfrme)
  71. new_tsk.pack(side=tk.TOP)
  72. messagebox.showinfo('eTasks', 'Task saved')
  73. except pyodbc.IntegrityError:
  74. messagebox.showinfo('eTasks', 'You already have a task with that name. Please change it.')
  75. #button to save the task to the db
  76. 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:

  1. Exception in Tkinter callback
  2. Traceback (most recent call last):
  3. File "C:\Program Files\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
  4. return self.func(*args)
  5. ^^^^^^^^^^^^^^^^
  6. File "C:\Users\ellio_6\Documents\etasks-main\src\Home.py", line 77, in newtaskwin
  7. TaskNameLbl=tk.Label(tasknameframe, text='Task Name:').grid(row=1, column=0, pady=5)
  8. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  9. File "C:\Program Files\Python311\Lib\tkinter\__init__.py", line 2549, in grid_configure
  10. self.tk.call(
  11. _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 的几何管理器应该放在新的一行,所以不是:

  1. tasknameframe=tk.Frame(newtaskwin, relief='groove', highlightbackground='black', highlightthickness=1, width=250).grid()

而应该是:

  1. tasknameframe=tk.Frame(newtaskwin, relief='groove',
  2. highlightbackground='black', highlightthickness=1, width=250)
  3. 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

  1. tasknameframe=tk.Frame(newtaskwin, relief='groove',
  2. highlightbackground='black', highlightthickness=1, width=250)
  3. tasknameframe.grid()

Thank you all that posted this in the comments!

huangapple
  • 本文由 发表于 2023年3月15日 20:46:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定