Python图形编程探索系列-01-初级任务

时间:2023-03-08 20:17:27

设计任务

设计一个主窗口,在其中添加三个标签和三个按钮,当点击按钮时,对标签的内容和色彩进行修改。

代码初步设计

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前

![](https://img2018.cnblogs.com/blog/1372901/201810/1372901-20181018091950934-608185290.jpg)

click后

![](https://img2018.cnblogs.com/blog/1372901/201810/1372901-20181018092153648-1989620658.jpg)

评价

本次代码基本完成功能,但是代码重复的部分很多,下一个版本进行优化。