Tkinter 使用“.grid”时出错(代码未完成)

问题描述 投票:0回答:1
Traceback (most recent call last):
  File "/Users/kalybarly/.py", line 22, in <module>
    arial.grid(row=0, column=0, sticky=tk.W+tk.E)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 2522, in grid_configure
    self.tk.call(
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack

...

import tkinter as tk

root = tk.Tk()

fnt = 'Arial'

root.title('Text Editor')
root.geometry('800x500')

ttl = tk.Label(root, text='Text Editor', font=('Arial', 25), height=3) 
ttl.pack(padx=10, pady=20)

text = tk.Text(root, font=(fnt, 16))
text.pack(padx=0, pady = 10)

button_frame = tk.Frame(root)
button_frame.columnconfigure(0, weight=1)
button_frame.columnconfigure(1, weight=1)
button_frame.columnconfigure(2, weight=1)

arial = tk.Button(root, text='Arial', font=('Arial',10), height=1)
arial.grid(row=0, column=0, sticky=tk.W+tk.E)

button_frame.pack(fill='x')

root.mainloop()
python user-interface tkinter
1个回答
0
投票

您遇到的错误是由于您尝试在同一父小部件(根)中使用包和网格几何管理器。在 Tkinter 中,每个父小部件只能使用一个几何管理器。

import tkinter as tk

root = tk.Tk()

fnt = 'Arial'

root.title('Text Editor')
root.geometry('800x500')

ttl = tk.Label(root, text='Text Editor', font=('Arial', 25), height=3) 
ttl.pack(padx=10, pady=20)

text = tk.Text(root, font=(fnt, 16))
text.pack(padx=0, pady = 10)

button_frame = tk.Frame(root)
button_frame.pack(fill='x')

arial = tk.Button(button_frame, text='Arial', font=('Arial',10), height=1)
arial.grid(row=0, column=0, sticky=tk.W+tk.E)

root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.