程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python, according to the Tkinter counter case, wrote a countdown timer

編輯:Python

Catalog

Preface :

One 、 Case counter

1、 design sketch

2、 Code

  3、 analysis

Two 、 Effect after tuning

1、 design sketch

​ edit  2、 Code

 3、 analysis

​ edit   3、 ... and 、 Disguised as a countdown timer

1、 effect

 2、 Code

 3、 analysis

  Four 、 summary

Preface :

         Recently, I occasionally have time to study python Of GUI, And I saw the big tail tkinter, then CSDN The government has also raised its status very high . Then give these reasons .

1、kinter There is an inherent advantage of : It can be used directly without installation
2、 Compare with wx or Qt Numerous controls and components ,Tk Only a dozen controls can meet almost all application needs

         It's really simple , Students with foundation , It took about a 1 You will be able to master it in hours . Ha ha ha , Don't talk much , Get to the point , Let's talk about how today's counter and countdown counter are reversed .

One 、 Case counter

         Still use our 3 Point positioning method , design sketch + Code + Analysis to laolaoha .

1、 design sketch

         It's a button plus a moving number , Every time you click the button, you will add 1, The default from the 0 Start . It's so simple and rough ~

2、 Code

from tkinter import *
class MyApp(Tk):
""" Inherit Tk, Create your own desktop application class """
def __init__(self):
""" Constructors """
super().__init__()
self.title(' Click on the counter... Button ')
self.geometry('320x160')
#self.iconbitmap('res/Tk.ico')# Icon , If you don't want to use the default icon, change it
self.counter = IntVar() # Create an integer variable object
self.counter.set(0) # Set its initial value to 0
label = Label(self, textvariable=self.counter, font=("Arial Bold", 50)) # take Label Associated with an integer variable object
label.pack(side='left', expand='yes', fill='both', padx=5, pady=5)
btn = Button(self, text=' Let me have a try ', bg='#90F0F0')
btn.pack(side='right', anchor='center', fill='y', padx=5, pady=5)
btn.bind(sequence='<Button-1>', func=self.on_button) # Binding events and event functions
def on_button(self, evt):
""" The response function of the click button event , evt It's the object of the event """
self.counter.set(self.counter.get() + 1)
if __name__ == '__main__':
app = MyApp()
app.mainloop()

  3、 analysis

Two 、 Effect after tuning

1、 design sketch

         You can customize the initial value , Go up according to the floor you want to go , Ha ha ha

 2、 Code

from tkinter import *
import time
class MyApp(Tk):
def __init__(self):
super().__init__()
self.title(' This is a counter ')# Set title
self.geometry('380x160')# Set canvas size
self.iconbitmap('res\Tk.ico')# Set the app icon
self.count_num=IntVar()# Create a Intvar Variable
self.count_num.set(a)# The initialization counter is the number of countdowns entered
lable=Label(self,textvariable=self.count_num,font=('Arial Bold',50))
lable.pack(side='left',padx=5,pady=5,fill='both',expand='yes')
bth=Button(self,text=' You look ',bg='#1AA7Ec',fg='white')
bth.pack(side='right',pady=5,ipadx=5,fill='y',anchor='center')
bth.bind(sequence='<Button-1>',func=self.button_on)
def button_on(self,evt):
self.count_num.set(self.count_num.get()+1)
#time.sleep(2)
#self.destroy()
if __name__=='__main__':
a = int(input(" Please enter the initial value of the counter :"))# Define global variables a, Used to save the count
app=MyApp()
app.mainloop()

 3、 analysis

  3、 ... and 、 Disguised as a countdown timer

1、 effect

 

 2、 Code

from tkinter import *
import time
class MyApp(Tk):
def __init__(self):
super().__init__()
self.title(' This is a countdown timer ')# Set title
self.geometry('480x160')# Set canvas size
self.iconbitmap('res\Tk.ico')# Set the app icon
self.count_num=IntVar()# Create a Intvar Variable
self.count_num.set(a)# The initialization counter is the number of countdowns entered
lable=Label(self,textvariable=self.count_num,font=('Arial Bold',50))
lable.pack(side='left',padx=5,pady=5,fill='both',expand='yes')
bth=Button(self,text=' You look ',bg='#1AA7Ec',fg='white')
bth.pack(side='right',pady=5,ipadx=5,fill='y',anchor='center')
bth.bind(sequence='<Button-1>',func=self.button_on)#Button-1 For left mouse click events
def button_on(self,evt):
global a
if a>0:
self.count_num.set(self.count_num.get()-1)
print(a)
a=a-1
else:
self.count_num.set(' The countdown is over ')
print(' The countdown is over !!')
#self.sleep()
#self.destroy()
if __name__=='__main__':
a = int(input(" Please enter the countdown number :"))# Define global variables a, Used to save the timing
app=MyApp()
app.mainloop()

 3、 analysis

         The above counters are added each time 1, Of course, I can reduce each time 1 了 , For example, we play , discharge PPT The countdown timer is used at the beginning , It seems that the counter can be used to compare the cubs , Study 1-100, Learn to 1, Then click to teach him 2. The modified key code is in button_on Function . Remember to reference global variables a When you remember , add global.

  Four 、 summary

         well , Today's sharing is here , When we study , We can draw inferences from one example , In this way, we will find learning suddenly interesting , I hope to make progress every day , backgammon . I wish good , If you don't understand, please leave a message for discussion . Or send me a private message ~


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved