gpt4 book ai didi

python - 为什么 tkinter 不让我使用 tk.StringVar()?

转载 作者:太空宇宙 更新时间:2023-11-04 01:30:21 25 4
gpt4 key购买 nike

我想知道为什么 tkinter 不让我使用它的 StringVar() 函数。

我代码的相关部分:

import tkinter as tk, os

font = ("Curlz MT", 14, "bold")
font2 = ("Curlz MT", 10, "bold")

GameName = tk.StringVar()

root = tk.Tk()
root.geometry('240x50+0+0')
root.title("Name?")

EyourName = tk.Entry(font=font, width=16, textvariable=GameName)
EyourName.insert(tk.END, "Waiting...")
EyourName.place(x=8,y=8)

Bok = tk.Button(font=font2, width=2, text="Ok", command=Ok)
Bok.place(x=200, y=8)

root.mainloop()

(完整程序在这里:http://www.2shared.com/file/nANE4rSD/Blah_Blah_Blah.html)

错误:

Traceback (most recent call last):

File "C:\Documents and Settings\SOMENAME\Desktop\David\Python\MonstaWorld\NewGame2.py",

line 34, in <module>

GameName = tk.StringVar()

File "C:\Python32\lib\tkinter\__init__.py", line 243, in __init__

Variable.__init__(self, master, value, name)

File "C:\Python32\lib\tkinter\__init__.py", line 174, in __init__

self._tk = master.tk

AttributeError: 'NoneType' object has no attribute 'tk'

最佳答案

您必须先初始化 tkinter,然后才能使用像 StringVars 这样的 tkinter 对象。

将行 root = tk.Tk() 从现在的位置移动到之前 GameName = tk.StringVar() 和问题将得到解决。

关于python - 为什么 tkinter 不让我使用 tk.StringVar()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14254481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com