Hướng dẫn button click python
Tkinter Button được sử dụng để thêm các button trong ứng dụng Python. Các button này có thể hiển thị văn bản hoặc hình ảnh để thể hiện mục đích của chúng. Bạn có thể gán một hàm hoặc một phương thức vào một button, hàm hoặc phương thức này tự động được gọi khi click vào button đó. Sau đây là cú pháp của Tkinter Button trong Python: Tham số
Sau đây là các phương thức thường được sử dụng cho Button trong Python:
Ví dụ Tkinter Button trong PythonVí dụ 1: from tkinter import * top = Tk() top.geometry("200x100") b = Button(top, text = "Button") b.pack() top.mainloop() Kết quả: Ví dụ 2: from tkinter import * from tkinter import messagebox top = Tk() top.geometry("200x100") def clickRedButton(): messagebox.showinfo("Hello", "Red Button clicked") def clickBlueButton(): messagebox.showinfo("Hello", "Blue Button clicked") b1 = Button(top, text = "Red", command = clickRedButton, activeforeground = "red", activebackground = "pink", pady = 10) b2 = Button(top, text = "Blue", command = clickBlueButton, activeforeground = "blue", activebackground = "pink", pady = 10) b3 = Button(top, text = "Green", activeforeground = "green", activebackground = "pink", pady = 10) b4 = Button(top, text = "Yellow", activeforeground = "yellow", activebackground = "pink", pady = 10) b1.pack(side = LEFT) b2.pack(side = RIGHT) b3.pack(side = TOP) b4.pack(side = BOTTOM) top.mainloop() Kết quả: Tkinter Button được sử dụng để thêm các button trong ứng dụng Python. Các button này có thể hiển thị văn bản hoặc hình ảnh để thể hiện mục đích của chúng. Bạn có thể gán một hàm hoặc một phương thức vào một button, hàm hoặc phương thức này tự động được gọi khi click vào button đó. Nội dung chính Cú phápSau đây là cú pháp của Tkinter Button trong Python: W = Button(parent, options) Tham số
Sau đây là các phương thức thường được sử dụng cho Button trong Python:
Ví dụ Tkinter Button trong PythonVí dụ 1: from tkinter import * top = Tk() top.geometry("200x100") b = Button(top, text = "Button") b.pack() top.mainloop() Kết quả: Ví dụ 2: from tkinter import * from tkinter import messagebox top = Tk() top.geometry("200x100") def clickRedButton(): messagebox.showinfo("Hello", "Red Button clicked") def clickBlueButton(): messagebox.showinfo("Hello", "Blue Button clicked") b1 = Button(top, text = "Red", command = clickRedButton, activeforeground = "red", activebackground = "pink", pady = 10) b2 = Button(top, text = "Blue", command = clickBlueButton, activeforeground = "blue", activebackground = "pink", pady = 10) b3 = Button(top, text = "Green", activeforeground = "green", activebackground = "pink", pady = 10) b4 = Button(top, text = "Yellow", activeforeground = "yellow", activebackground = "pink", pady = 10) b1.pack(side = LEFT) b2.pack(side = RIGHT) b3.pack(side = TOP) b4.pack(side = BOTTOM) top.mainloop() Kết quả: |