为什么我的tkinter按钮没有显示,我是否已在模块中安装了所有内容,如果不能安装它?

时间:2017-10-24 08:23:53

标签: 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()

1 个答案:

答案 0 :(得分:4)

好的,我们走了。

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

Pygame与tkinter无关,你在这里没有导入pygame,你正在导入tkinter。

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

不,不是。 Frame小部件就是窗口内的一个框架。它本身并没有画出一个窗口。您的示例中的参数Frame根本不是Frame小部件,它的值是Tk(),它是在tkinter中绘制第一个窗口的函数。

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

我实际上对你在这里尝试做的事感到茫然。 master应该等于Frame,等于screen等于Tk(),但如果我是正确的,那么你要覆盖它并告诉它等于None

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

我不认为你在做你认为你在这里做的事情。 This answer比我更好地解释了它。

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

如果我正确地阅读你的程序,那么永远不会调用window.__init__window(),所以这个功能都不会真正发生。

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

您正试图致电.pack() self.pack()正在Frame上呼叫self。通常情况下,我们不会以这种方式为self分配值(尽管这是有效的),read this以找出应该使用#placing the button stop = Button(self, master, message= "Stop") 的内容。

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)

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

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

window

你在这里做的是调用班级window.__init__(),在你当前的程序中所做的就是调用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() ,这本身就什么都没有。

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

我相信您在计划中尝试做的事情如下:

<div class="row">
   <div class="col-md-3"></div>
   <div class="col-md-6"></div>
   <div class="col-md-3"></div>
</div>