#提问在注释里
import tkinter as tk
from tkinter import messagebox
def count():
global counter#这里为什么要定义global
counter += 1
def show():
messagebox.showinfo("","counter=" + str(counter) + ",state=" + str(switch.get()))#这里为什么不先定义global switch和global counter
window = tk.Tk()
switch = tk.IntVar()
counter = 0
button = tk.Button(window, text="Show", command=show)
button.pack()
checkbutton = tk.Checkbutton(window, text="Tick", variable=switch, command=count)
checkbutton.pack()
window.mainloop()
```