Liên kết trong python tkinter

Trong phần này, chúng ta sẽ tìm hiểu về một số widget cơ bản trong Tkinter là

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
0,
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
1,
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
2, và 
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
3

Widget là các thành phần cấu trúc nên một ứng dụng GUI. Widget rất đa dạng, có một số widget quan trọng cần phải có của bất kỳ nền tảng nào nào kể cả Tkinter ví dụ như nút [nút bấm], hộp kiểm hoặc thanh cuộn [thanh cuộn]. Ngoài những widget cơ bản lập trình viên còn có thể tùy chỉnh widget của riêng mình

nút kiểm

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
0 là tiện ích hiển thị hộp đánh dấu

from tkinter import Tk, Frame, Checkbutton
from tkinter import BooleanVar, BOTH

class Example[Frame]:
  def __init__[self, parent]:
    Frame.__init__[self, parent]
 
    self.parent = parent
    self.initUI[]
 
  def initUI[self]:
    self.parent.title["Checkbutton"]
    self.pack[fill=BOTH, expand=True]
    self.var = BooleanVar[]
    
    cb = Checkbutton[self, text="Show Title", variable=self.var, command=self.onClick]
    cb.select[]
    cb.place[x=50, y=50]
 
  def onClick[self]:
    if self.var.get[] == True:
      self.master.title["Checkbutton"]
    else:
      self.master.title[""]
 
root = Tk[]
root.geometry["250x150+300+300"]
app = Example[root]
root.mainloop[]

Trong ví dụ trên chúng ta hiển thị một nút kiểm tra lên cửa sổ có chức năng hiện/ẩn tiêu đề cửa sổ khi kiểm tra

self.var = BooleanVar[]

Ở trên chúng ta tạo ra một đối tượng 

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
5đối tượng này sẽ được kết nối với nút kiểm, mỗi khi trạng thái kiểm tra thay đổi thì giá trị của
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
6 cũng sẽ thay đổi

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]

At the lines on them ta create a

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
0 object. Chúng ta kết nối biến
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
6 thông qua tham số
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
9. Tham số
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
30 chỉ định phương thức nào sẽ được gọi khi kiểm tra, ở đây là phương thức
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
31

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
3

Chúng ta sử dụng phương thức

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
32 để thiết lập trạng thái cho nút kiểm tra

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
7

Tại phương thức

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
33 chúng ta cho hiện/ẩn tiêu đề cửa sổ thông qua phương thức
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
34, nút kiểm tra chúng ta kiểm tra có được kiểm tra hay không thông qua thuộc tính
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
6

Nhãn mác

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
1 dùng để hiển thị văn bản hoặc hình ảnh. Trong ví dụ dưới đây, chúng ta sẽ sử dụng Nhãn để hiển thị ảnh lên màn hình

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
2

Để chạy được ví dụ trên thì bạn phải có thư viện

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
37 trong Python

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
4

Mặc định thì

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
1 trong Tkinter cũng có các mô-đun có thể hiển thị ảnh nhưng chế độ rất hạn chế

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
6

Chúng ta sử dụng tham số

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
39 khi khởi tạo để gán ảnh vào nhãn

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
8

Tiếp theo bạn phải tạo một biến để giữ lại các tham chiếu đến ảnh nếu không bộ thu thập tài nguyên của Python sẽ xóa mất ảnh của chúng ta

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
9

Hai dòng mã trên chúng ta cho kích thước màn hình bằng đúng với kích thước ảnh

Tỉ lệ

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
2 là tiện ích hiển thị một thanh cuộn gắn với một khoảng giá trị bất kỳ

self.var = BooleanVar[]
0

Trong ví dụ trên chúng ta cho hiển thị một

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
71 và một
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
72, văn bản trên nhãn sẽ thay đổi khi kéo thanh cuộn trên
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
71

self.var = BooleanVar[]
1

Create a object

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
74Hai tham số 
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
75và 
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
76là phạm vi giá trị của quy mô. Tham số
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
30 chỉ định phương thức sẽ được gọi khi di chuyển thanh cuộn

self.var = BooleanVar[]
2

Cũng giống như Checkbutton, ở đây chúng ta tạo nhãn, một đối tượng

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
78 và kết nối các đối tượng đó với
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
72 bằng tham số
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
20. Giá trị của biến này sẽ được hiển thị trên nhãn

self.var = BooleanVar[]
3

Khi di chuyển thanh cuộn, phương thức 

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
21 sẽ được gọi kèm theo giá trị hiện tại của
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
2. Chúng ta chuyển đổi kiểu dữ liệu của tham số này từ sang
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
23 rồi sang
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
24 sau đó đặt giá trị của đối tượng
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
78 và phần văn bản hiển thị trên nhãn sẽ thay đổi theo

hộp danh sách

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
3 cho phép hiển thị một danh sách các mục. Người dùng có thể chọn một hoặc nhiều mục

self.var = BooleanVar[]
4

Trong ví dụ trên, chúng tôi hiển thị một danh sách tên các diễn viên nữ trong

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
3. Tên được chọn sẽ được hiển thị trên một nhãn

self.var = BooleanVar[]
5

Chúng ta tạo ra một đối tượng L

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
28và thêm các mục vào bằng phương thức 
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
29

self.var = BooleanVar[]
6

Khi chọn một mục thì sự kiện

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
40 sẽ được thực hiện. Chúng ta gán sự kiện này vào phương thức
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
41

self.var = BooleanVar[]
7

Trong phương thức

cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
42 them ta lấy tham chiếu đến
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
3 thông qua thuộc tính
cb = Checkbutton[self, text="Show title", variable=self.var, command=self.onClick]
44và gán vào biến ____245

Chủ Đề