Tkinter无法识别任何类型的函数

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

Tkinter not recognizing any type of function

问题

import tkinter
import customtkinter

tk = tkinter.Tk(className='Tkinter - TutorialKart', )
tk.geometry("500x300")

def submitFunction():
  q1 = tk.Label(text="Hello, Tkinter")
  q1.pack

button_submit = tkinter.Button(tk, text="Submit", command=submitFunction)
button_submit.config(width=20, height=2)

button_submit.pack()
tk.mainloop()

如您所见,通过运行此代码并将变量 q1 更改为其他函数,它将无法工作,我会收到以下错误:

AttributeError: '_tkinter.tkapp' object has no attribute 'Label'
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__    
    return self.func(*args)
  File "c:\Users\USER\OneDrive - Bristol Virginia Public Schools\Desktop\Current projects\main.py", line 8, in submitFunction
    q1 = tk.Label(text="Hello, Tkinter")
  File "C:\Users\tpitcock\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2383, in __getattr__ 
    return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'Label'

我尝试将文本更改为不同的内容,但不起作用。

我正在使用Python 3。

英文:
import tkinter
import customtkinter

tk = tkinter.Tk(className='Tkinter - TutorialKart', )
tk.geometry("500x300")

def submitFunction() :
  q1 = tk.Label(text="Hello, Tkinter")
  q1.pack


button_submit = tkinter.Button(tk, text ="Submit", command=submitFunction)
button_submit.config(width=20, height=2)

button_submit.pack()
tk.mainloop()

As you can see by running the code and editing the variable q1 to any other function it will not work, I get this error:

AttributeError: '_tkinter.tkapp' object has no attribute 'Label'
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__    
    return self.func(*args)
  File "c:\Users\USER\OneDrive - Bristol Virginia Public Schools\Desktop\Current projects\main.py", line 8, in submitFunction
    q1 = tk.Label(text="Hello, Tkinter")
  File "C:\Users\tpitcock\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2383, in __getattr__ 
    return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'Label'

I tried changing text to different things but it didn't work.

I'm using Python 3.

答案1

得分: 1

错误提示告诉你出了什么问题。它说tk没有一个名为Label的属性,这是真的。在你的代码中,tk不是tkinter模块,而是你用这一行创建的Tk的实例:

tk = tkinter.Tk(className='Tkinter - TutorialKart', )

如果你想使用tkinter的Label类,它需要来自tkinter模块,就像你用tkinter.Tktkinter.Button一样:

q1 = tkinter.Label(text="Hello, Tkinter")
#    ^^^^^^^
英文:

The error is telling you what is wrong. It says that tk doesn't have an attribute named Label, which is true. In your code, tk isn't the tkinter module, it's the instance of Tk that you created with this line:

tk = tkinter.Tk(className='Tkinter - TutorialKart', )

If you want to use the tkinter Label class, it needs to come from the tkinter module just like you do with tkinter.Tk and tkinter.Button:

q1 = tkinter.Label(text="Hello, Tkinter")
#    ^^^^^^^

答案2

得分: -2

你的目标是什么?你只是想创建一个窗口吗?如果是的话,首先你需要用from tkinter import *导入tkinter中的所有类。然后你可以从Tk类创建一个对象:Favorite name = Tk()。然后,你可以使用这个对象访问tkinter中的所有函数和类。下面的代码是一个简单的窗口设计:

from tkinter import *
window = Tk()
window.title("你喜欢的")
window.geometry("700x700")
.
.
.
window.mainloop()
英文:

what is your goal? Do you just want to make a window? If yes, first you need to import all of the classes in tkinter with from tkinter import *. And after that you can make an object from Tk class: Favorite name = Tk(). Then you can access all functions and classes in tkinter using this object. The code below is a simple design of a window:

from tkinter import *
window = Tk()
window.title("your favorite")
window.geometry("700x700")
.
.
.
window.mainloop()

huangapple
  • 本文由 发表于 2023年3月31日 22:07:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899469.html
匿名

发表评论

匿名网友

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

确定