繁体   English   中英

使用tkinter在python中进行GUI编程时出错

[英]Error in gui programming in python using tkinter

#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

import Tkinter

class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent=parent
    def initialize(self):
        self.grid()

        self.entry=Tkinter.Entry(self)
        self.entry.grid(column=0,row=0,sticky='EW')
        self.entry.bind("<Return>",self.OnPressEnter)

        button=Tkinter.Button(self,test="Post it!",command=self.OnButtonClick)
        button.grid(column=1,row=0)

        label=Tkinter.Label(self,anchor="w",fg="white",bg="blue")
        label=grid(column=0,row=1,columnspan=2,sticky='EW')

        self.grid_columnconfigure(0,weight=1)

    def OnButtonClick(self):
        print "you clicked the button!"

    def OnPressEnter(self,event):
        print "you pressed enter!"

if __name__=="__main__":
    app=simpleapp_tk(None)
    app.title('poster')
    app.mainloop()

我编写该程序的目的是为了制作一个框来输入文本和一个按钮,但是除了窗口外,它什么也没有显示。 错误在哪里?

主要问题是您忘记了调用app.initialize() ,但是您也遇到了一些错字。 我已经指出了此固定版本中的注释。

import Tkinter

class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent=parent
    def initialize(self):
        self.grid()

        self.entry=Tkinter.Entry(self)
        self.entry.grid(column=0,row=0,sticky='EW')
        self.entry.bind("<Return>",self.OnPressEnter)

        button=Tkinter.Button(self,text="Post it!",command=self.OnButtonClick)
        # the text keyword argument was mis-typed as 'test'

        button.grid(column=1,row=0)

        label=Tkinter.Label(self,anchor="w",fg="white",bg="blue")
        label.grid(column=0,row=1,columnspan=2,sticky='EW')
        # the . in label.grid was mis-typed as '='

        self.grid_columnconfigure(0,weight=1)

    def OnButtonClick(self):
        print "you clicked the button!"

    def OnPressEnter(self,event):
        print "you pressed enter!"

if __name__=="__main__":
    app=simpleapp_tk(None)
    app.title('poster')
    app.initialize() # you forgot this
    app.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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