(複製到記事本比較好看)
#-*- coding: UTF-8 -*-
import tkinter as tk #大小寫要注意,如果小寫不行就改大寫
import time
a = True
b = True
def st():
#使用global的目地是為了在方法中改變a的值,同時可以同步改變外面a的值,就是全域變數、區域變數的差別
global a
if a:
#改變按鈕顏色
button_start.configure(bg='blue')
a = False
else:
button_start.configure(bg='gray94')
a = True
def ed():
global b
if b:
button_end.configure(bg='red')
b = False
else:
button_end.configure(bg='gray94')
b = True
#創建一個視窗
top = tk.Tk()
#視窗名稱
top.title('GUI')
#寬:300高:200的視窗,放在寬:600高:300的位置
top.geometry('300x200+600+300')
label_1 = tk.Label()
button_start = tk.Button(top,text = 'start',bd=4,height=4,width=22,bg ='gray94',fg='blue',command =st)
button_end = tk.Button(top,text = 'end',bd=4,height=4,width=22,bg ='gray94',command =ed)
button_start.grid(padx=10, pady=10, sticky="nw")
button_end.grid(padx=10, pady=20, sticky="nw")
top.mainloop() #執行視窗
-------------------------------------------------------------------------------
top:顯示在哪個視窗
text:按鈕文字'視窗
bd:按鈕外框的陰影
height:長
width:寬
bg:背景顏色
fg:字的顏色
command:按下按鈕所執行的方法(不用加括號)