简体   繁体   中英

Why is my tkinter button not displaying, have I installed everything in the module if not how can I install it?

#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()

Ok, here we go.

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

Pygame has nothing to do with tkinter and you're not importing pygame here, you're importing tkinter.

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

No, it isn't. A Frame widget is just that, a frame inside a window. It doesn't draw a window in and of itself. The parameter Frame in your example isn't even a Frame widget at all, it's value is Tk() which is the function called to draw the first window in tkinter.

def __init__(self, master = None):#The master widget defines the settings upon initialisation

I'm actually at a loss for what you're attempting to do here. master should equal Frame which equals screen which equals Tk() but if I'm correct you're overriding that and telling it to equal None ?

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

I don't think you're doing what you think you're doing here. This answer explains it better than I could.

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

If I'm reading your program correctly then window.__init__window() is never called, so none of this function ever actually happens.

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

You're attempting to call .pack() on self which is calling .pack() on Frame . Typically we wouldn't assign a value to self in this way (although this is valid), read this to find out what self should be used for.

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

This isn't placing the Button widget, this is assigning the Button widget to a variable. Also, Button widgets are declared as Button(parent, *attributes) and there is no message attribute built in. Meaning what you meant to call was Button(self.master, text="Stop") .

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

This is where you're placing the button, but the function that contains this is never called, so it never happens.

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

What you're actually doing here is calling the class window , all this does in your current program is call window.__init__() , which in itself does essentially nothing.


This is meant with no offence but I think you're lacking a very basic understanding of tkinter and possibly even Pythonic OOP.

I believe what you're trying to do in your program is the below:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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