Python 为什么我的tkinter按钮没有显示,我是否已安装模块中的所有内容?如果没有,如何安装?

Python 为什么我的tkinter按钮没有显示,我是否已安装模块中的所有内容?如果没有,如何安装?,python,tkinter,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

好了,开始吧

#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()
Pygame与tkinter无关,您不是在这里导入Pygame,而是在导入tkinter

from tkinter import * #pygame is the module that has collections of functions that is used to create a window import everything from tkinter
不,不是。
Frame
小部件就是窗口中的一个框架。它本身并没有画出一扇窗户。示例中的参数
Frame
根本不是
Frame
小部件,它的值是
Tk()
,该函数用于在tkinter中绘制第一个窗口

class window(Frame): #Frame comes from tkinter is what you think as a window
事实上,我对你在这里试图做的事感到不知所措
master
应该等于
Frame
它等于
screen
它等于
Tk()
,但是如果我是正确的,你是在覆盖它,并告诉它等于
None

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
如果我正确地读取了您的程序,则
窗口。\uuu init\uu window()
从未被调用,因此此函数从未实际发生过

def __init__window(self):  #creation of the init window
您正试图在
self
上调用
.pack()
,它正在
框架上调用
.pack()
。通常,我们不会以这种方式(尽管这是有效的)给
self
赋值,以找出
self
应该用于什么

self.pack(fill=BOTH, expand=1)#The pack function will pack this in our frame
这不是放置
按钮
小部件,而是将
按钮
小部件分配给变量。另外,
按钮
小部件被声明为
按钮(父,*属性)
并且没有内置的
消息
属性。意思是你要调用的是
按钮(self.master,text=“Stop”)

这是您放置按钮的地方,但是包含该按钮的函数从未被调用,因此它从未发生过

#intialising the button that will start the stopwatch
stop.place(x=0, y=0)
实际上您在这里所做的是调用类
窗口
,在当前程序中所做的就是调用
窗口


这并不意味着冒犯,但我认为您对tkinter甚至Python OOP缺乏非常基本的理解

我相信您在计划中要做的是:

app = window(screen) #The app variable is assigned to the window creation which has the tkinter module

标题不好,为什么要添加这么多命令?看一看,这可能会对您有所帮助。实际上,如果继承框架的主框架设置为有效的父框架(如Tk根),则
self.pack
会起作用,这可能是他们的目的。对不起,我想我已经说得够清楚了。在他们的示例中,
self
不是tkinter小部件。因此,对其调用
.pack()
不会有任何作用。事实上,窗口继承了作为tkinter小部件的框架,因此
self.pack
是有效的。
app = window(screen) #The app variable is assigned to the window creation which has the tkinter module
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()