ch4_1.py
from tkinter import *
cnt=1
def msgShow():
global cnt
label["text"] = "I love Python x" + str(cnt)
label["bg"] = "lightyellow"
label["fg"] = "blue"
# label.config(text="I love Python x" + str(cnt),
# bg="lightyellow",fg="blue")
cnt += 1
root = Tk()
root.title("ch4_1")
label = Label(root)
# label["text"] = "I love Java"
btn = Button(root,text="打印消息",command=msgShow)
label.pack()
btn.pack()
root.mainloop()
ch4_2.py
from tkinter import *
cnt=1
def msgShow():
global cnt
# label["text"] = "I love Python x" + str(cnt)
# label["bg"] = "lightyellow"
# label["fg"] = "blue"
label.config(text="I love Python x" + str(cnt),
bg="lightyellow",fg="blue")
cnt += 1
root = Tk()
root.title("ch4_2")
label = Label(root)
# label["text"] = "I love Java"
btn = Button(root,text="打印消息",command=msgShow)
label.pack()
btn.pack()
root.mainloop()
ch4_3.py
from tkinter import *
cnt=1
def msgShow():
global cnt
# label["text"] = "I love Python x" + str(cnt)
# label["bg"] = "lightyellow"
# label["fg"] = "blue"
label.config(text="I love Python x" + str(cnt),
bg="lightyellow",fg="blue")
cnt += 1
root = Tk()
root.title("ch4_3")
label = Label(root)
# label["text"] = "I love Java"
btn1 = Button(root,text="打印消息",width=15,command=msgShow)
btn2 = Button(root,text="结束",width=15,command=root.destroy)
label.pack()
btn1.pack(side=LEFT)
btn2.pack(side=LEFT)
root.mainloop()
ch4_4.py
from tkinter import *
counter = 0 # 计数的全局变量
def run_counter(digit): # 数字变量内容的更新
def counting(): # 更新数字方法
global counter # 定义全局变量
counter += 1
digit.config(text=str(counter)) # 列出数字内容
# counter += 1
digit.after(1000,counting) # 隔一秒后调用counting
counting() # 持续调用
root = Tk()
root.title("ch4_4")
digit = Label(root,bg="yellow",fg="blue",
height=3,width=10,
font="Helvetic 20 bold")
digit.pack()
run_counter(digit)
Button(root,text="结束",width=15,command=root.destroy).pack(pady=10)
root.mainloop()
ch4_5_1.py
from tkinter import *
def bColor(bgColor):
root.config(bg=bgColor)
root = Tk()
root.title("ch4_5_1")
root.geometry("300x200")
# 以此新建三个按钮
exitbtn = Button(root,text="Exit",command=root.destroy)
bluebtn = Button(root,text="Blue",command=lambda : bColor("blue"))
yellowbtn = Button(root,text="Yellow",command=lambda : bColor("yellow"))
# 将三个按钮包装定位在右下方
exitbtn.pack(anchor=S,side=RIGHT,padx=5,pady=5)
bluebtn.pack(anchor=S,side=RIGHT,padx=5,pady=5)
yellowbtn.pack(anchor=S,side=RIGHT,padx=5,pady=5)
root.mainloop()
ch4_5.py
from tkinter import *
def yellow():
root.config(bg="yellow")
def blue():
root.config(bg="blue")
root = Tk()
root.title("ch4_5")
root.geometry("300x200")
# 以此新建三个按钮
exitbtn = Button(root,text="Exit",command=root.destroy)
bluebtn = Button(root,text="Blue",command=blue)
yellowbtn = Button(root,text="Yellow",command=yellow)
# 将三个按钮包装定位在右下方
exitbtn.pack(anchor=S,side=RIGHT,padx=5,pady=5)
bluebtn.pack(anchor=S,side=RIGHT,padx=5,pady=5)
yellowbtn.pack(anchor=S,side=RIGHT,padx=5,pady=5)
root.mainloop()
ch4_6.py
from tkinter import *
cnt=1
def msgShow():
global cnt
# label["text"] = "I love Python x" + str(cnt)
# label["bg"] = "lightyellow"
# label["fg"] = "blue"
label.config(text="I love Python x" + str(cnt),
bg="lightyellow",fg="blue")
cnt += 1
root = Tk()
root.title("ch4_6")
root.geometry("600x600")
label = Label(root)
# label["text"] = "I love Java"
sunGif = PhotoImage(file="1_cropped.png")
btn = Button(root,image=sunGif,command=msgShow)
label.pack()
btn.pack()
root.mainloop()
ch4_7.py
from tkinter import *
cnt=1
def msgShow():
global cnt
# label["text"] = "I love Python x" + str(cnt)
# label["bg"] = "lightyellow"
# label["fg"] = "blue"
label.config(text="I love Python x" + str(cnt),
bg="lightyellow",fg="blue")
cnt += 1
root = Tk()
root.title("ch4_6")
root.geometry("600x600")
label = Label(root)
# label["text"] = "I love Java"
myPic = PhotoImage(file="1_cropped.png")
btn = Button(root,image=myPic,command=msgShow,
text="Click Me",compound=TOP)
label.pack()
btn.pack()
root.mainloop()
ch4_8.py
from tkinter import *
cnt=1
def msgShow():
global cnt
# label["text"] = "I love Python x" + str(cnt)
# label["bg"] = "lightyellow"
# label["fg"] = "blue"
label.config(text="I love Python x" + str(cnt),
bg="lightyellow",fg="blue")
cnt += 1
root = Tk()
root.title("ch4_6")
root.geometry("600x600")
label = Label(root)
# label["text"] = "I love Java"
myPic = PhotoImage(file="1_cropped.png")
btn = Button(root,image=myPic,command=msgShow,
text="Click Me",compound=CENTER)
label.pack()
btn.pack()
root.mainloop()
ch4_9.py
from tkinter import *
cnt=1
def msgShow():
global cnt
# label["text"] = "I love Python x" + str(cnt)
# label["bg"] = "lightyellow"
# label["fg"] = "blue"
label.config(text="I love Python x" + str(cnt),
bg="lightyellow",fg="blue")
cnt += 1
root = Tk()
root.title("ch4_6")
root.geometry("600x600")
label = Label(root)
# label["text"] = "I love Java"
myPic = PhotoImage(file="1_cropped.png")
btn = Button(root,image=myPic,command=msgShow,
text="Click Me",compound=LEFT)
label.pack()
btn.pack()
root.mainloop()
ch4_10.py
from tkinter import *
root = Tk()
root.title("ch4_10")
lab = Label(root,text="",bg="yellow",width=20)
btn7 = Button(root,text="7",width=3)
btn8 = Button(root,text="8",width=3)
btn9 = Button(root,text="9",width=3)
btnM = Button(root,text="*",width=3) # 乘法符号
btn4 = Button(root,text="4",width=3)
btn5 = Button(root,text="5",width=3)
btn6 = Button(root,text="6",width=3)
btnS = Button(root,text="-",width=3) # 减法符号
btn1 = Button(root,text="1",width=3)
btn2 = Button(root,text="2",width=3)
btn3 = Button(root,text="3",width=3)
btnP = Button(root,text="+",width=3) # 加法符号
btn0 = Button(root,text="0",width=9)
btnD = Button(root,text=".",width=3) # 小数点符号
btnE = Button(root,text="=",width=3)
lab.grid(row=0,column=0,columnspan=4)
btn7.grid(row=1,column=0,padx=5)
btn8.grid(row=1,column=1,padx=5)
btn9.grid(row=1,column=2,padx=5)
btnM.grid(row=1,column=3,padx=5) # 乘法符号
btn4.grid(row=2,column=0,padx=5)
btn5.grid(row=2,column=1,padx=5)
btn6.grid(row=2,column=2,padx=5)
btnS.grid(row=2,column=3,padx=5) # 减法符号
btn1.grid(row=3,column=0,padx=5)
btn2.grid(row=3,column=1,padx=5)
btn3.grid(row=3,column=2,padx=5)
btnP.grid(row=3,column=3,padx=5) # 加法符号
btn0.grid(row=4,column=0,padx=5,columnspan=2)
btnD.grid(row=4,column=2,padx=5) # 小数点符号
btnE.grid(row=4,column=3,padx=5)
root.mainloop()
ch4_11.py
from tkinter import *
cnt=1
def msgShow():
global cnt
# label["text"] = "I love Python x" + str(cnt)
# label["bg"] = "lightyellow"
# label["fg"] = "blue"
label.config(text="I love Python x" + str(cnt),
bg="lightyellow",fg="blue")
cnt += 1
root = Tk()
root.title("ch4_11")
root.geometry("600x600")
label = Label(root)
# label["text"] = "I love Java"
myPic = PhotoImage(file="1_cropped.png")
btn = Button(root,image=myPic,command=msgShow,
cursor="star")
label.pack()
btn.pack()
root.mainloop()