10.1Python Graphical User Interface library Tkinter (1): Hello Tkinter__python

Source: Internet
Author: User
Tags event listener pack

The @ Overview TK is a (available for multi-platform) GUI library; Tkinter is the interface of the Python language to access the library and the standard for Python's GUI development;

@ First GUI interface

From tkinter Import *

# Create a program window
= Tk ()

# Create a control instance
lable = Label (
    window,  # parent Container
    text= Hello "  # optional keyword parameters (see source note)
button = button (
    window,  # parent container
    text=" Click Me "  # Optional keyword parameters (see source comment) # to

package the control in the parent container
lable.pack ()
button.pack ()

# dead loop waiting for user action
Window.mainloop () Here to write a code piece

@ Handle Button Events

# Place the button in the window and set its Click event
# create Window
= Tk ()

# Create two instances
Btnok = button (
    window, text= "OK", Foreground= "Red",
    Command=dealbtnok  # button Click event Listener, point to a response function name, do not add (), otherwise syntactically incorrect
)
btncancel = button (
    window, text= "Cancel", background= "Red", 
    command=dealbtncancel 
)

# to package the instance into the parent container
Btnok.pack ()
btncancel.pack ()

# Opens the message (dead) loop (waits for user input)
window.mainloop ()

Button Event Listener function

# Processing button click event
def dealbtnok ():
    print ("Dealbtnok")


# Handle button click event
def dealbtncancel ():
    print (" Dealbtncancel ")

@ Refactoring the above code in an object-oriented way

# Encapsulate window and button events as Class
Eventprocessbutton ():
    # Add Windows and Event Listener
    def __init__ (self) In initialization method: Window
        = Tk ()
        Btnok = button (window, text= "OK", foreground= "Red", Command=self.processbtnok)
        btncancel = button (window, text= " Cancel ", background=" Red ", Command=self.processbtncancel)
        btnok.pack ()
        btncancel.pack
        () Window.mainloop ()
        Pass

    # event listener is also encapsulated in the class
    def Processbtnok (self):
        print ("Processbtnok")
        Pass

    # Event Listening functions are also encapsulated in the class
    def processbtncancel (self):
        print ("Processbtncancel")
        Pass

Create an instance of a class to implement a Window button event listener

Eventprocessbutton ()

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.