Getting started with Python: Hello

Source: Internet
Author: User

Getting started with Python: Hello

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()

This code shows how to use Python to create a graphical interface. The graphical interface library Tkinter in python is used.

Import the corresponding module: from Tkinter import * defines a class that inherits the 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()
Define controls
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"})
Create an object and use
root = Tk()app = Application(master = root)app.mainloop()root.destroy()

In the code snippet above, root represents the window container. We create an Application object, specify its window container, enable the Message Queue loop, and finally destroy the window.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.