Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

The countdown widget is very useful, if you are a student, you can play around with this widget. I wrote it in Python +Tkinter.

Tkinter is a standard package for Python, so we don’t need to install it. We’ll start by creating a window, and then we’ll add some widgets on top of it, like buttons, check boxes, and so on, and use some of their properties. Without further ado, let’s begin!

#! /usr/bin/python3 import tkinter top = tkinter.tk ()Copy the code
  • 1. Import Tkinter module
  • 2. Create controls
  • 3. Specify the master of the control, which control belongs to
  • 4, Tell the GM(Geometry Manager) that a control has been generated.

As shown in the figure above, a box appears, in which case we use the Tkinter module.

def __init__(self,master): frame = Frame(master) frame.pack() self.entryWidget = Entry(frame) self.entryWidget["width"] = 15 Pack (side=LEFT) self. entrywidget. pack(side=LEFT) self.hi_there = Button(frame, text=" start ", Pack (side=LEFT) self. Button = button (frame, text=" exit ", fg="red", command=frame.quit) self.button.pack(side=LEFT)Copy the code

First initialize the data and window data, then create two new buttons, start and exit. The above function __init__ initializes the class in which all attributes of the class should be written. Let’s examine the controls from the Tkinter module above. Button(frame, text=” start “, command=self.start) generates a Button, the frame inside is tkinter.tk (), the text text displayed above the Button, and the response function is self.start. Pressing the button triggers the start function of the current class.

def start(self): text = self.entryWidget.get().strip() if text ! = "": num = int(text) self.countDown(num)Copy the code

This is the start button trigger, using the Entry control. It reads the string in text, forces it to an integer second if the string is not empty, and passes it to countDown.

def countDown(self,seconds): lbl1.config(bg='yellow') lbl1.config(height=3, font=('times', 20, 'bold')) for k in range(seconds, 0, -1): if k == 30: print("\a") if k== 29: print("\a") if k== 28: print("\a") lbl1["text"] = k root.update() time.sleep(1) lbl1.config(bg='red') lbl1.config(fg='white') lbl1["text"] = "Time's up! Tkmessagebox.showinfo (" Time up!" "Time's up!" )Copy the code

CountDown counts the number of seconds, which is a loop with a step of -1, and then uses time.sleep(1) to make it continue every second. At the end of the day, I run out of time, modify the background, popover time.

The Tkinter is recognized by this little thing up here. Basically, using Python to do programs with an interface will think of Tkinter, if you need to go to learn. Of course, there are more useful controls, such as Listbox, Menu, Radiobutton and more controls can be learned.

You are welcome to discuss procedural questions with me and answer any questions you may have. Follow the public number: poetic code, make a friend. Leave a comment if you think it’ll help.