This article is participating in Python Theme Month. See the link for details

Hello everyone, today I found another interesting gadget. I’m an old poet of fun gadgets.

I believe many people feel boring when they start to learn programming. They always think, why do we program against black and white boxes? Doesn’t it have a graphical interface? Yes, programming with a graphical interface should be more fun. The poem will take you through the graphical programming of Python.

Know Tkinter

The Kinter module (Tk interface) is an interface to Python’s standard Tk GUI toolkit.Tk and Tkinter can be used on most Unix platforms, as well as Windows and Macintosh systems. Later versions of Tk8.0 can implement a native windowing style and run well on most platforms.

Since Tkinter is built into the Python installation package, the Tkinter library can be imported once Python is installed, and IDLE is written in Tkinter, Tkinter can handle simple graphical interfaces very well

import tkinter
Copy the code

No installation required

The first code

#! /usr/bin/python3 import tkinter top = tkinter.tk ()Copy the code

Code run screenshot:

Add button control

#! /usr/bin/python # -*- coding: UTF-8 -*- import Tkinter import tkMessageBox top = Tkinter.Tk() def helloCallBack(): Tkmessagebox.showinfo ("Hello Python", "Hello Runoob") B = tkkinter. Button(top, text =" Button ", command = helloCallBack) B.pack() top.mainloop()Copy the code

The tkMessageBox pop-up package is added as shown in the code above. Then the code adds buttons.

B = tkinter. Button(top, text =” Button “, command = helloCallBack)

The button says “click me”. And the response function is helloCallBack.

So when you press the button, it calls helloCallBack. And the helloCallBack calls the message popover the name is the first one, and then the content is the second one.

The running effect is as follows:

Of course thinter has many controls, not just Button.

Canvas Canvas control; Display graphic elements such as lines or text Checkbutton checkbox controls; Used to provide multiple choice box Entry control in the program; Frame Frame control for displaying simple text content; Displays a rectangular area on the screen, used mostly as a container Label control. Can display text and bitmap Listbox controls; The Listbox widget is used to display a list of strings to the user of the Menubutton Menubutton control, which is used to display menu items. Menu Menu control; Display menu bar, drop-down menu and pop-up menu Message control; Used to display multiple lines of text, similar to the Radiobutton Radiobutton control label; Displays a radio button state Scale range control; A Scrollbar control that displays a numeric scale for a limited range of output. It is used when the content exceeds the visual area, such as a list box. .text Text control; Used to display multiple lines of text

For a more systematic look at GUI programming in Python (Tkinter), please go to the public account: Poetic Code. Now that we’re in. Just give me a “like” and leave.