Python圖形編程探索系列-01-初級

  • 2020 年 1 月 19 日
  • 筆記

設計任務

設計一個主窗口,在其中添加三個標籤和三個按鈕,當點擊按鈕時,對標籤的內容和色彩進行修改。

程式碼初步設計

import tkinter as tk    root = tk.Tk()    def f1():      label1.config(text='點我,我加油了,哈哈', bg='#A23400')    def f2():      label2.config(text='successful', bg='#000093')    def f3():      label3.config(text='peculiar', bg='#C4C400')    label1 = tk.Label(root, text='標籤1', fg='red', bg='#6C6C6C')  label1.pack(anchor=tk.NE, ipadx=0.2, ipady=0.2)    label2 = tk.Label(root, text='標籤2', fg='white', bg='#6C6C6C')  label2.pack(anchor=tk.NE, ipadx=0.2, ipady=0.2)    label3 = tk.Label(root, text='標籤3', fg='white', bg='#6C6C6C')  label3.pack(anchor=tk.NE, padx=0.5, pady=0.5)      button1 = tk.Button(root, text='按鈕1', command=f1)  button1.pack(anchor=tk.NW)    button2 = tk.Button(root, text='按鈕2', command=f2)  button2.pack(anchor=tk.NW)    button3 = tk.Button(root, text='按鈕3', command=f3)  button3.pack(anchor=tk.NW)    root.mainloop()

結果

click前

click後

評價

本次程式碼基本完成功能,但是程式碼重複的部分很多,下一個版本進行優化。