我想讓這段代碼加一而不是顯示時間。我已經嘗試了一切,但只是遇到了很多錯誤。有人可以指出我正確的方向嗎?
# importing whole module
from tkinter import *
from tkinter.ttk import *
# importing strftime function to
# retrieve system's time
from time import strftime
# creating tkinter window
root = Tk()
root.title('Clock')
# This function is used to
# display time on the label
def time():
string = strftime('%H:%M:%S %p')
lbl.config(text = string)
lbl.after(1000, time)
# Styling the label widget so that clock
# will look more attractive
lbl = Label(root, font = ('calibri', 40, 'bold'),
background = 'purple',
foreground = 'white')
# Placing clock at the centre
# of the tkinter window
lbl.pack()
time()
mainloop()
uj5u.com熱心網友回復:
簡單地增加一個整數?你不需要時間模塊。作為猜測:
# importing whole module
from tkinter import *
from tkinter.ttk import *
# creating tkinter window
root = Tk()
root.title('Clock')
# This function is used to
# display time on the label
def time(counter_var=0):
lbl.config(text = counter_var)
lbl.after(1000, time, counter_var 1)
# Styling the label widget so that clock
# will look more attractive
lbl = Label(root, font = ('calibri', 40, 'bold'),
background = 'purple',
foreground = 'white')
# Placing clock at the centre
# of the tkinter window
lbl.pack()
time()
mainloop()
順便說一句,通配符匯入(您稱之為“匯入整個模塊”)是已知的錯誤來源,并且違反了 python 的官方 PEP8 樣式指南。我推薦這個標準匯入:
import tkinter as tk
from tkinter import ttk
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/323752.html
