简体   繁体   English

将窗口定义为类,但无法调用mainloop函数

[英]Defined a window as a class, but unable to call mainloop function

I am so trash at coding that I have no idea how to utilise classes to make cool stuff :( --- I'm really new to GUI development, and I'm trying to make a simple maze game with a level selector. I have the maze program squared away, but I am somehow hopeless at Tkinter apparently, since I've been trying constantly for the last hour to find a solution online. As you might have noticed, this is my first post here. 我在编码方面实在是太浪费了,我不知道如何利用类来制作很酷的东西:(--我真的是GUI开发的新手,我正在尝试用关卡选择器制作一个简单的迷宫游戏。迷宫程序摆平了,但是显然我对Tkinter毫无希望,因为我在最后一个小时一直在努力寻找在线解决方案,您可能已经注意到,这是我在这里的第一篇文章。

I'm running this in PyCharm, using my decent computer on Windows 10. I'm especially trash at this IDE since I, for some reason, cannot install any libraries/ use any libraries that I see clearly installed in my list of libraries... but that's for another post. 我正在Windows 10上使用像样的计算机在PyCharm中运行此程序。由于某些原因,我无法安装任何库/无法使用我在库列表中明确安装的任何库,因此在此IDE上尤其如此。 ..但这是另一篇文章。 As I've mentioned, I've been trying to figure out a simple program for the past hour, but nothing seems to be working. 正如我所提到的,过去一个小时我一直在尝试找出一个简单的程序,但是似乎没有任何效果。

Nothing I find online is particularly useful, either, and the ones that might be are so hopelessly complex that I cannot understand what they are trying to achieve. 我发现网上也没有什么特别有用的,而且那些可能过于复杂以至于我无法理解他们正在努力实现的目标。 I'm looking for a simple solution to a simple problem, and hopefully, this great community can help me out. 我正在寻找一个简单问题的简单解决方案,希望这个伟大的社区能够为我提供帮助。

import tkinter as tk


class Window():
    def __init__(self):
        self = tk.Tk()
        self.geometry("%dx%d+0+0" % (1920,1080))


root = Window()

root.mainloop()

Expected: Window appears Observed: Program abruptly ends 预期:出现窗口观察到:程序突然结束

Error:
Traceback (most recent call last):
  File "C:/Users/(GD) ShadowPlague/PycharmProjects/GameDesign/Main.py", line 12, in <module>
    root.mainloop()
AttributeError: 'Window' object has no attribute 'mainloop'

You create class in wrong way. 您以错误的方式创建了类。 You can't assign Tk() to self to create correctly class. 您不能将Tk()分配给self来正确创建类。 External root will have nothing to do with internal self . 外在的root与内在的self无关。 First you create instance Window() and assign to variable root but later you create instance Tk() and assign to self but it will not change instance assigned to root . 首先,您创建实例Window()并分配给变量root但是随后您创建实例Tk()并分配给self但它不会更改分配给root实例。


First method: create Tk() inside class as self.root and then you use win.root 第一种方法:在类内部将Tk()创建为self.root ,然后使用win.root

import tkinter as tk

class Window():

    def __init__(self):
        self.root = tk.Tk()
        self.root.geometry("%dx%d+0+0" % (1920,1080))

win = Window()

win.root.mainloop()

Second method: inherit from Tk() . 第二种方法:从Tk()继承。 It needs Window(tk.Tk) and super().__init__ 它需要Window(tk.Tk)super().__init__

import tkinter as tk

class Window(tk.Tk):

    def __init__(self):
        super().__init__()
        self.geometry("%dx%d+0+0" % (1920,1080))

root = Window()

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM