【问题标题】:Why is my tkinter button not displaying, have I installed everything in the module if not how can I install it?为什么我的 tkinter 按钮不显示,我是否安装了模块中的所有内容,如果没有,我该如何安装?
【发布时间】:2017-10-24 08:23:53
【问题描述】:
#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()

【问题讨论】:

  • 标题不好,为什么要添加这么多命令?
  • 看看这个how to也许对你有帮助。

标签: python tkinter


【解决方案1】:

好的,我们开始吧。

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

您正在尝试在 self 上调用 .pack(),而在 Frame 上调用 .pack()。通常我们不会以这种方式为self 赋值(尽管这是有效的),read this 以找出self 应该用于什么。

#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__(),它本身基本上什么都不做。


这无意冒犯,但我认为您缺乏对 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()

【讨论】:

  • 实际上self.pack 会工作,如果继承框架的主人被设置为一个有效的父母,如 Tk 根,这可能是他们想要的。
  • 抱歉,我想我已经说得够清楚了。在他们的示例中,self 不是 tkinter 小部件。所以打电话给.pack(),不会做任何事情。
  • 就是,window 继承自 Frame 是一个 tkinter 小部件,所以self.pack 是有效的。
猜你喜欢
  • 2015-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 2018-05-10
  • 2021-12-27
  • 2019-09-28
  • 1970-01-01
相关资源
最近更新 更多