Python Tkinter模块Mainloop()

Python Tkinter模块Mainloop(),python,tkinter,Python,Tkinter,因此,我在Pycharm中运行此代码时遇到问题。文件名为Python.py 以下是我遇到的错误: Traceback (most recent call last): File "/Users/gabriel/PycharmProjects/SpamBot/SpamBotCode.py", line 3, in <module> screen = Tk() NameError: name 'Tk' is not defined Indentatio

因此,我在Pycharm中运行此代码时遇到问题。文件名为Python.py 以下是我遇到的错误:

 Traceback (most recent call last):
  File "/Users/gabriel/PycharmProjects/SpamBot/SpamBotCode.py", line 3, in <module>
    screen = Tk()
NameError: name 'Tk' is not defined

IndentationError: unexpected indent

您需要执行
screen.mainloop()
,如下所示:

def main_screen():
    screen = tk.Tk()
    screen.geometry("300x250")
    screen.title("Spam Bot 1.0 ")

    # this part is the gui (Graphical user interface)

    # this is the background to be more precise

    bgImage = PhotoImage(file=r"background.png")
    Label(screen, image=bgImage).place(relwidth=1, relheight=1)

    Label(text="Welcome To Spam Bot 1.0", bg="green", height="2", width="300").pack()
    Label(text="").pack()

    Button(text="Beemovie Spam ", height="2", width="300").pack()
    Label(text="").pack()
    Button(text="Other", height="2", width="300").pack()
    Label(text="").pack()
    Button(text="Other", height="2", width="300").pack()
    screen.mainloop()

main_screen()
按照您的方式,您调用了定义为
mainloop
的函数,该函数显然没有定义。另外,请重命名
Python.py
,因为它会导致名称冲突,因此某些库将无法导入/工作

还提到,
mainloop
应该是您最后调用的东西,因此在该函数中定义
screen
不是最好的主意。在调用
main\u screen
之前定义它,将
screen
作为参数传递,并在函数完成后执行mainloop

编辑 通过tkinter导入的
*

from tkinter import *

screen = Tk()
screen.geometry("300x250")
screen.title("Spam Bot 1.0 ")

Label(text="Welcome To Spam Bot 1.0", bg="green", height="2", width="300").pack()
Label(text="").pack()

Button(text="Beemovie Spam ", height="2", width="300").pack()
Label(text="").pack()
Button(text="Other", height="2", width="300").pack()
Label(text="").pack()
Button(text="Other", height="2", width="300").pack()

screen.mainloop()

您需要执行
screen.mainloop()
。Mainloop也是一个阻塞调用,所以应该被称为last。为什么要将GUI编写为函数呢?如果GUI中的复杂性增加,则很难解决此问题。@Atlas435是的,实际上我刚刚删除了它。您需要将代码放在一边,如果您正在输入本地范围,请仅使用制表位或4个空格。另外请注意,在StackOverflow中,我们不使用线程来询问多个问题。现在看看答案,它已经不合适了。评论不适合进行长时间的讨论;这段对话已经结束。
from tkinter import *

screen = Tk()
screen.geometry("300x250")
screen.title("Spam Bot 1.0 ")

Label(text="Welcome To Spam Bot 1.0", bg="green", height="2", width="300").pack()
Label(text="").pack()

Button(text="Beemovie Spam ", height="2", width="300").pack()
Label(text="").pack()
Button(text="Other", height="2", width="300").pack()
Label(text="").pack()
Button(text="Other", height="2", width="300").pack()

screen.mainloop()