为什么我的tkinter按钮不显示,如果没有安装模块中的所有组件,该如何安装?

-3 python tkinter

#snakes and ladder
from tkinter import * #pygame is the module that has collections of functions that is used to create a window import everything from tkinter
import time

class window(Frame): #Frame comes from tkinter is what you think as a window

    def __init__(self, master = None):#The master widget defines the settings upon initialisation
        Frame.__init__(self, master) #This is called the frame class that has been built in python
        self.master = master

    def __init__window(self):  #creation of the init window

        self.master.title("Reagan Kambayi") #It changes the title of the title of our widget

        self.pack(fill=BOTH, expand=1)#The pack function will pack this in our frame

        #placing the button
        stop = Button(self, master, message= "Stop")

        #intialising the button that will start the stopwatch
        stop.place(x=0, y=0)




screen = Tk() #It must be written with capitalised T because there will be an error and it holds the components of the tkinter module
screen.geometry("700x500")
app = window(screen) #The app variable is assigned to the window creation which has the tkinter module

screen.mainloop()
Run Code Online (Sandbox Code Playgroud)

Eth*_*eld 5

好的,我们开始。

from tkinter import * #pygame is the module that has collections of functions that is used to create a window import everything from tkinter
Run Code Online (Sandbox Code Playgroud)

Pygame与tkinter无关,您不是在这里导入pygame,而是在导入tkinter。

class window(Frame): #Frame comes from tkinter is what you think as a window
Run Code Online (Sandbox Code Playgroud)

不,不是。甲Frame插件就是这样,窗口内的帧。它本身并不会绘制一个窗口。Frame您的示例中的参数根本不是Frame小部件,它的值是Tk()调用此函数以在tkinter中绘制第一个窗口的值。

def __init__(self, master = None):#The master widget defines the settings upon initialisation
Run Code Online (Sandbox Code Playgroud)

实际上,您在这里尝试做的事情让我感到茫然。master应该等于Frame哪个等于screen哪个相等,Tk()但是如果我正确的话,您要覆盖它并告诉它等于None

Frame.__init__(self, master) #This is called the frame class that has been built in python
Run Code Online (Sandbox Code Playgroud)

我不认为您在做您认为在这里所做的事情。这个答案比我能解释的更好。

def __init__window(self):  #creation of the init window
Run Code Online (Sandbox Code Playgroud)

如果我正确地阅读了您的程序,则window.__init__window()永远不会调用它,因此实际上不会发生任何此功能。

self.pack(fill=BOTH, expand=1)#The pack function will pack this in our frame
Run Code Online (Sandbox Code Playgroud)

你试图调用.pack()self被调用.pack()Frame。通常,我们不会以self这种方式分配值(尽管这是有效的),请仔细阅读该内容以了解self应使用的值。

#placing the button
stop = Button(self, master, message= "Stop")
Run Code Online (Sandbox Code Playgroud)

这不是放置Button小部件,这是分配Button小部件的变量。此外,Button小部件被声明为,Button(parent, *attributes)并且没有message内置属性。这意味着您要调用的是Button(self.master, text="Stop")

#intialising the button that will start the stopwatch
stop.place(x=0, y=0)
Run Code Online (Sandbox Code Playgroud)

在这里放置按钮,但是永远不会调用包含此按钮的函数,因此它永远不会发生。

app = window(screen) #The app variable is assigned to the window creation which has the tkinter module
Run Code Online (Sandbox Code Playgroud)

您实际上在这里执行的是调用该类window,在当前程序中所有这些操作都是call window.__init__(),它本身实际上什么也不做。


这意味着没有冒犯,但我认为您对tkinter甚至Pythonic OOP缺乏非常基本的了解。

我相信您要在程序中执行的操作如下:

from tkinter import *

class App:
    def __init__(self, root):
        self.root = root
        self.root.title("Reagan Kambayi")
        self.stop = Button(self.root, text="Stop")
        self.stop.place(x=0, y=0)

root = Tk()
App(root)

root.mainloop()
Run Code Online (Sandbox Code Playgroud)