Python入門案例之Hello

來源:互聯網
上載者:User

標籤:tkinter   python   

from Tkinter import *class Application(Frame):    def say_hi(self):        print ‘hello‘    def createWiegets(self):        self.QUIT = Button(self)        self.QUIT["text"] = "QUIT"        self.QUIT["fg"] = "red"        self.QUIT["command"] = self.quit        self.QUIT.pack({"side":"left"})        self.hi_there = Button(self)        self.hi_there["text"] = "hello"        self.hi_there["command"] = self.say_hi        self.hi_there.pack({"side":"left"})    def __init__(self,master=None):        Frame.__init__(self,master)        self.pack()        self.createWiegets()root = Tk()app = Application(master = root)app.mainloop()root.destroy()

這段代碼說明了如何使用Python來建立一個圖形介面。使用的是python中的Tkinter這個圖形介面庫。

  • 匯入相應的模組:from Tkinter import *
  • 定義一個類,繼承Frame
class Application(Frame):    def say_hi(self):        print ‘hello‘    def createWiegets(self):        self.QUIT = Button(self)        self.QUIT["text"] = "QUIT"        self.QUIT["fg"] = "red"        self.QUIT["command"] = self.quit        self.QUIT.pack({"side":"left"})        self.hi_there = Button(self)        self.hi_there["text"] = "hello"        self.hi_there["command"] = self.say_hi        self.hi_there.pack({"side":"left"})    def __init__(self,master=None):        Frame.__init__(self,master)        self.pack()        self.createWiegets()
  • 定義控制項
def createWiegets(self):        self.QUIT = Button(self)        self.QUIT["text"] = "QUIT"        self.QUIT["fg"] = "red"        self.QUIT["command"] = self.quit        self.QUIT.pack({"side":"left"})        self.hi_there = Button(self)        self.hi_there["text"] = "hello"        self.hi_there["command"] = self.say_hi        self.hi_there.pack({"side":"left"})
  • 建立對象並使用
root = Tk()app = Application(master = root)app.mainloop()root.destroy()

在上面的程式碼片段中,root是代表的是視窗的容器,我們建立Application對象,並且指定了它的視窗容器,開啟訊息佇列迴圈,最後銷毀視窗。

Python入門案例之Hello

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.