簡體   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