test.py 905 B

123456789101112131415161718192021222324252627282930313233
  1. #使用python构建一个UI页面 上面有3个按钮
  2. #点击按钮1,弹出toast提示“点击了按钮1”
  3. #点击按钮2,弹出toast提示“点击了按钮2”
  4. #点击按钮3,弹出toast提示“点击了按钮3”
  5. import tkinter as tk
  6. from tkinter import messagebox
  7. class ToastApp:
  8. def __init__(self):
  9. self.root = tk.Tk()
  10. self.root.title("Button Demo")
  11. self.root.geometry("300x200")
  12. # Create and pack buttons
  13. for i in range(3):
  14. btn = tk.Button(
  15. self.root,
  16. text=f"Button {i + 1}",
  17. command=lambda x=i + 1: self.show_toast(x)
  18. )
  19. btn.pack(pady=10)
  20. def show_toast(self, button_num):
  21. messagebox.showinfo("Toast", f"Clicked Button {button_num}")
  22. def run(self):
  23. self.root.mainloop()
  24. if __name__ == "__main__":
  25. app = ToastApp()
  26. app.run()