Python 圖形介面:小時鐘

畫面

程式碼

# -*- coding: utf-8 -*- import kivy from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label from kivy.uix.button import Button from kivy.config import Config from kivy.clock import Clock import time class MyTimerApp(App): running = False layoutScreen = None labelMessage = None buttonOK = None mingFont = None def setup(self): self._app_name = 'Hello Kivy' self.title = 'Timer' Config.set('graphics', 'width', '160') Config.set('graphics', 'height', '100') kivy.resources.resource_add_path('/Library/Fonts/Microsoft') self.mingFont = kivy.resources.resource_find('MingLiU.ttf') def showTime(self, instance): if self.running: self.labelMessage.text = time.strftime('%H:%M:%S', time.localtime(time.time())) def controlClock(self, instance): self.running = not self.running if self.running: self.buttonOK.text = u'停止' Clock.schedule_interval(self.showTime, 0.1) else: self.labelMessage.text = u'時間' self.buttonOK.text = u'啟動' Clock.unschedule(self.showTime) def build(self): self.setup() self.layoutScreen = BoxLayout(orientation='vertical') self.labelMessage = Label() self.labelMessage.text = u'時間' self.labelMessage.font_name = self.mingFont self.buttonOK = Button() self.buttonOK.text = u'啟動' self.buttonOK.font_name = self.mingFont self.buttonOK.bind(on_press=self.controlClock) self.layoutScreen.add_widget(self.labelMessage) self.layoutScreen.add_widget(self.buttonOK) return self.layoutScreen if __name__ == '__main__': MyTimerApp().run()

進階版

# -*- coding: utf-8 -*- import kivy from kivy.app import App from kivy.config import Config from kivy.clock import Clock from kivy.uix.boxlayout import BoxLayout from kivy.uix.image import Image from kivy.uix.label import Label from kivy.uix.button import Button import time class MyTimerApp(App): running = False layoutScreen1 = None layoutScreen2 = None imagePng = None labelMessage = None buttonOK = None mingFont = None def setup(self): self._app_name = 'Hello Kivy' self.title = 'Timer' Config.set('graphics', 'width', '320') Config.set('graphics', 'height', '120') kivy.resources.resource_add_path('/Library/Fonts/Microsoft') self.mingFont = kivy.resources.resource_find('MingLiU.ttf') def showTime(self, instance): if self.running: self.labelMessage.text = time.strftime('%H:%M:%S', time.localtime(time.time())) def controlClock(self, instance): self.running = not self.running if self.running: self.buttonOK.text = u'停止' Clock.schedule_interval(self.showTime, 0.1) else: self.labelMessage.text = u'時間' self.buttonOK.text = u'啟動' Clock.unschedule(self.showTime) def build(self): self.setup() self.layoutScreen1 = BoxLayout(orientation='horizontal') self.layoutScreen2 = BoxLayout(orientation='vertical') self.imagePng = Image() self.imagePng.source = '/Users/BizAnalytics/Pictures/EagleEye.png' self.labelMessage = Label() self.labelMessage.text = u'時間' self.labelMessage.font_name = self.mingFont self.buttonOK = Button() self.buttonOK.text = u'啟動' self.buttonOK.font_name = self.mingFont self.buttonOK.bind(on_press=self.controlClock) self.layoutScreen2.add_widget(self.labelMessage) self.layoutScreen2.add_widget(self.buttonOK) self.layoutScreen1.add_widget(self.imagePng) self.layoutScreen1.add_widget(self.layoutScreen2) return self.layoutScreen1 if __name__ == '__main__': MyTimerApp().run()