Làm cách nào để mở tệp tkinter trong python?

Python Tkinter (và TK) cung cấp một bộ hộp thoại mà bạn có thể sử dụng khi làm việc với tệp. Bằng cách sử dụng những thứ này, bạn không phải tự thiết kế các hộp thoại tiêu chuẩn. Các hộp thoại mẫu bao gồm hộp thoại mở tệp, hộp thoại lưu tệp và nhiều hộp thoại khác. Bên cạnh hộp thoại tệp còn có các hộp thoại tiêu chuẩn khác, nhưng trong bài viết này chúng tôi sẽ tập trung vào hộp thoại tệp

Show

Hộp thoại tệp giúp bạn mở, lưu tệp hoặc thư mục. Đây là loại hộp thoại bạn nhận được khi bấm vào tệp, mở. Hộp thoại này ra khỏi mô-đun, không cần phải viết tất cả mã theo cách thủ công

Tkinter không có hộp thoại tệp tìm kiếm gốc, thay vào đó, nó có kiểu tk khách hàng. Bạn có thể thấy những điều dưới đây

Hộp thoại tệp sẽ hoạt động trên tất cả các nền tảng máy tính để bàn

khóa học liên quan. Ứng dụng máy tính để bàn Python với Tkinter

hộp thoại tập tin

hộp thoại tập tin tkinter

Hộp thoại tệp tkinter có nhiều loại. Loại bạn cần thực sự phụ thuộc vào nhu cầu ứng dụng của bạn. Tất cả chúng đều là các cuộc gọi phương thức

Bạn có thể mở một tệp, một thư mục, lưu dưới dạng tệp và hơn thế nữa. Mỗi hộp thoại được tạo với ví dụ bên dưới là một loại hộp thoại khác nhau

1
2
3
4
5
6
7
8
9
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()

Làm cách nào để mở tệp tkinter trong python?

Bạn có thể tạo hộp thoại mở tệp yêu cầu tên tệp, sau đó trả về tên của hộp thoại đã chọn

Python có rất nhiều khung GUI, nhưng Tkinter là khung duy nhất được tích hợp trong thư viện chuẩn Python. Tkinter có một số điểm mạnh. Đó là nền tảng chéo, vì vậy cùng một mã hoạt động trên Windows, macOS và Linux. Các phần tử trực quan được kết xuất bằng cách sử dụng các phần tử hệ điều hành gốc, vì vậy các ứng dụng được xây dựng bằng Tkinter trông giống như chúng thuộc về nền tảng nơi chúng chạy

Although Tkinter is considered the de facto Python GUI framework, it’s not without criticism. One notable criticism is that GUIs built with Tkinter look outdated. If you want a shiny, modern interface, then Tkinter may not be what you’re looking for

However, Tkinter is lightweight and relatively painless to use compared to other frameworks. This makes it a compelling choice for building GUI applications in Python, especially for applications where a modern sheen is unnecessary, and the top priority is to quickly build something that’s functional and cross-platform

In this tutorial, you’ll learn how to

  • Get started with Tkinter with a Hello, World application
  • Work with widgets, such as buttons and text boxes
  • Control your application layout with geometry managers
  • Make your applications interactive by associating button clicks with Python functions

Ghi chú. Hướng dẫn này được điều chỉnh từ chương “Giao diện người dùng đồ họa” của Python Basics. Giới thiệu thực tế về Python 3

Cuốn sách sử dụng trình soạn thảo IDLE tích hợp sẵn của Python để tạo và chỉnh sửa các tệp Python cũng như tương tác với trình bao Python. Trong hướng dẫn này, các tham chiếu đến IDLE đã bị xóa để chuyển sang ngôn ngữ tổng quát hơn

Phần lớn tài liệu trong hướng dẫn này không thay đổi và bạn sẽ không gặp vấn đề gì khi chạy mã ví dụ từ trình chỉnh sửa và môi trường bạn chọn

Khi bạn đã thành thạo các kỹ năng này bằng cách thực hiện các bài tập ở cuối mỗi phần, bạn sẽ kết hợp mọi thứ lại với nhau bằng cách xây dựng hai ứng dụng. Đầu tiên là bộ chuyển đổi nhiệt độ và thứ hai là trình soạn thảo văn bản. Đã đến lúc đi sâu vào tìm hiểu cách xây dựng ứng dụng với Tkinter

Tiền thưởng miễn phí. 5 Suy nghĩ về Làm chủ Python, một khóa học miễn phí dành cho các nhà phát triển Python cho bạn thấy lộ trình và tư duy mà bạn sẽ cần để đưa các kỹ năng Python của mình lên một tầm cao mới

Lấy bài kiểm tra. Kiểm tra kiến ​​thức của bạn với bài kiểm tra tương tác “Lập trình GUI Python với Tkinter” của chúng tôi. Sau khi hoàn thành, bạn sẽ nhận được điểm số để có thể theo dõi quá trình học tập của mình theo thời gian

Lấy bài kiểm tra "

Xây dựng ứng dụng GUI Python đầu tiên của bạn với Tkinter

Yếu tố nền tảng của Tkinter GUI là cửa sổ. Windows là nơi chứa tất cả các thành phần GUI khác sống. Các phần tử GUI khác này, chẳng hạn như hộp văn bản, nhãn và nút, được gọi là tiện ích con. Widget được chứa bên trong cửa sổ

Đầu tiên, tạo một cửa sổ chứa một widget duy nhất. Bắt đầu một phiên trình bao Python mới và làm theo

Ghi chú. Các ví dụ mã trong hướng dẫn này đều đã được thử nghiệm trên Windows, macOS và Ubuntu Linux 20. 04 với Python phiên bản 3. 10

Nếu bạn đã cài đặt Python với bộ cài đặt chính thức có sẵn cho Windows và macOS từ python. org, thì bạn sẽ không gặp vấn đề gì khi chạy mã mẫu. Bạn có thể yên tâm bỏ qua phần còn lại của ghi chú này và tiếp tục với phần hướng dẫn

Nếu bạn chưa cài đặt Python bằng trình cài đặt chính thức hoặc không có bản phân phối chính thức nào cho hệ thống của bạn, thì đây là một số mẹo để thiết lập và sử dụng

Python trên macOS với Homebrew

Bản phân phối Python cho macOS có sẵn trên Homebrew không đi kèm với phần phụ thuộc Tcl/Tk theo yêu cầu của Tkinter. Phiên bản hệ thống mặc định được sử dụng thay thế. Phiên bản này có thể đã lỗi thời và ngăn bạn nhập mô-đun Tkinter. Để tránh sự cố này, hãy sử dụng trình cài đặt macOS chính thức

UbuntuLinux 20. 04

Để tiết kiệm dung lượng bộ nhớ, phiên bản mặc định của trình thông dịch Python được cài đặt sẵn trên Ubuntu Linux 20. 04 không hỗ trợ Tkinter. Tuy nhiên, nếu bạn muốn tiếp tục sử dụng trình thông dịch Python đi kèm với hệ điều hành của mình, thì hãy cài đặt gói sau

$ sudo apt-get install python3-tk

Thao tác này sẽ cài đặt mô-đun Python GUI Tkinter

Hương vị Linux khác

Nếu bạn không thể cài đặt Python hoạt động trên phiên bản Linux của mình, thì bạn có thể xây dựng Python với phiên bản Tcl/Tk chính xác từ mã nguồn. Để biết hướng dẫn từng bước về quy trình này, hãy xem Hướng dẫn thiết lập và cài đặt Python 3. Bạn cũng có thể thử sử dụng pyenv để quản lý nhiều phiên bản Python

Khi trình bao Python của bạn mở, điều đầu tiên bạn cần làm là nhập mô-đun Python GUI Tkinter

>>>

>>> import tkinter as tk

Một cửa sổ là một thể hiện của lớp

>>> import tkinter as tk
02 của Tkinter. Hãy tiếp tục và tạo một cửa sổ mới và gán nó cho biến
>>> import tkinter as tk
03

>>>

>>> window = tk.Tk()

Khi bạn thực thi đoạn mã trên, một cửa sổ mới hiện lên trên màn hình của bạn. Nó trông như thế nào phụ thuộc vào hệ điều hành của bạn

Làm cách nào để mở tệp tkinter trong python?

Trong suốt phần còn lại của hướng dẫn này, bạn sẽ thấy ảnh chụp màn hình Windows

Loại bỏ các quảng cáo

Thêm một tiện ích

Bây giờ bạn đã có một cửa sổ, bạn có thể thêm tiện ích. Sử dụng lớp

>>> import tkinter as tk
04 để thêm một số văn bản vào cửa sổ. Tạo một tiện ích
>>> import tkinter as tk
05 với văn bản
>>> import tkinter as tk
06 và gán nó cho một biến có tên là
>>> import tkinter as tk
07

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
1

Cửa sổ bạn đã tạo trước đó không thay đổi. Bạn vừa tạo một tiện ích

>>> import tkinter as tk
05, nhưng bạn chưa thêm nó vào cửa sổ. Có một số cách để thêm widget vào cửa sổ. Ngay bây giờ, bạn có thể sử dụng phương pháp
>>> import tkinter as tk
10 của tiện ích
>>> import tkinter as tk
05

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
5

Cửa sổ bây giờ trông như thế này

Làm cách nào để mở tệp tkinter trong python?

Khi bạn đóng gói một tiện ích vào một cửa sổ, Tkinter sẽ điều chỉnh kích thước cửa sổ nhỏ nhất có thể trong khi vẫn bao gồm đầy đủ tiện ích. Bây giờ thực hiện như sau

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
6

Dường như không có gì xảy ra, nhưng lưu ý rằng không có lời nhắc mới nào xuất hiện trong trình bao

>>> import tkinter as tk
11 yêu cầu Python chạy vòng lặp sự kiện Tkinter. Phương pháp này lắng nghe các sự kiện, chẳng hạn như bấm nút hoặc nhấn phím và chặn bất kỳ mã nào chạy sau nó cho đến khi bạn đóng cửa sổ nơi bạn đã gọi phương thức. Hãy tiếp tục và đóng cửa sổ bạn đã tạo và bạn sẽ thấy một lời nhắc mới được hiển thị trong trình bao

Cảnh báo. Khi bạn làm việc với Tkinter từ REPL của Python, các bản cập nhật cho cửa sổ được áp dụng khi mỗi dòng được thực thi. Đây không phải là trường hợp khi chương trình Tkinter được thực thi từ tệp Python

Nếu bạn không bao gồm

>>> import tkinter as tk
11 ở cuối chương trình trong tệp Python, thì ứng dụng Tkinter sẽ không bao giờ chạy và sẽ không có gì hiển thị. Ngoài ra, bạn có thể xây dựng giao diện người dùng của mình dần dần trong Python REPL bằng cách gọi
>>> import tkinter as tk
13 sau mỗi bước để phản ánh thay đổi

Tạo một cửa sổ với Tkinter chỉ mất một vài dòng mã. Nhưng các cửa sổ trống không hữu ích lắm. Trong phần tiếp theo, bạn sẽ tìm hiểu về một số tiện ích có sẵn trong Tkinter và cách bạn có thể tùy chỉnh chúng để đáp ứng nhu cầu của ứng dụng của mình

Kiểm tra việc hiểu của bạn

Mở rộng các khối mã bên dưới để kiểm tra sự hiểu biết của bạn

Tập thể dục. Tạo cửa sổ TkinterHiển thị/Ẩn

Viết một tập lệnh Python đầy đủ để tạo cửa sổ Tkinter với văn bản

>>> import tkinter as tk
14

Cửa sổ sẽ trông như thế này

Làm cách nào để mở tệp tkinter trong python?

Hãy thử bài tập này ngay bây giờ

Bạn có thể mở rộng khối mã bên dưới để xem giải pháp

Giải pháp. Tạo cửa sổ TkinterHiển thị/Ẩn

Đây là một giải pháp khả thi

$ sudo apt-get install python3-tk
1

Hãy nhớ rằng mã của bạn có thể trông khác

Khi bạn đã sẵn sàng, bạn có thể chuyển sang phần tiếp theo

Làm việc với Widget

Widget là cốt lõi của Python GUI framework Tkinter. Chúng là những yếu tố mà qua đó người dùng tương tác với chương trình của bạn. Mỗi widget trong Tkinter được định nghĩa bởi một lớp. Dưới đây là một số vật dụng có sẵn

Lớp Widget Mô tả

>>> import tkinter as tk
05Một tiện ích được sử dụng để hiển thị văn bản trên màn hình
>>> import tkinter as tk
16Một nút có thể chứa văn bản và có thể thực hiện một hành động khi được nhấp vào
>>> import tkinter as tk
17Một tiện ích nhập văn bản chỉ cho phép một dòng văn bản
>>> import tkinter as tk
18Một tiện ích nhập văn bản cho phép nhập văn bản nhiều dòng
>>> import tkinter as tk
19Một vùng hình chữ nhật được sử dụng để nhóm các tiện ích liên quan hoặc cung cấp

Bạn sẽ thấy cách làm việc với từng tiện ích này trong các phần sau, nhưng hãy nhớ rằng Tkinter có nhiều tiện ích hơn những tiện ích được liệt kê ở đây. Sự lựa chọn của tiện ích thậm chí còn phức tạp hơn khi bạn tính đến một bộ tiện ích theo chủ đề hoàn toàn mới. Tuy nhiên, trong phần còn lại của hướng dẫn này, bạn sẽ chỉ sử dụng các widget cổ điển của Tkinter

Nếu bạn muốn tìm hiểu thêm về hai loại tiện ích này, thì bạn có thể mở rộng phần có thể thu gọn bên dưới

Tiện ích cổ điển so với tiện ích theo chủ đềHiển thị/Ẩn

Điều đáng chú ý là hiện tại có hai loại tiện ích mở rộng trong Tkinter

  1. vật dụng cổ điển. Có sẵn trong gói
    >>> import tkinter as tk
    
    20, ví dụ như
    >>> import tkinter as tk
    
    21
  2. widget theo chủ đề. Có sẵn trong mô hình con
    >>> import tkinter as tk
    
    22, ví dụ như
    >>> import tkinter as tk
    
    23

Các tiện ích cổ điển của Tkinter có khả năng tùy biến cao và đơn giản, nhưng chúng có xu hướng lỗi thời hoặc hơi xa lạ trên hầu hết các nền tảng hiện nay. Nếu bạn muốn tận dụng lợi thế của các tiện ích có giao diện gốc và cảm giác quen thuộc với người dùng của một hệ điều hành nhất định, thì bạn có thể muốn xem các tiện ích theo chủ đề

Hầu hết các tiện ích theo chủ đề gần như là sự thay thế thả xuống cho các đối tác kế thừa của chúng, nhưng với giao diện hiện đại hơn. Bạn cũng có thể sử dụng một vài tiện ích hoàn toàn mới, chẳng hạn như thanh tiến trình, trước đây không có sẵn trong Tkinter. Đồng thời, bạn sẽ cần tiếp tục sử dụng một số tiện ích cổ điển không có tùy chọn thay thế theo chủ đề

Ghi chú. Các widget theo chủ đề trong mô-đun

>>> import tkinter as tk
24 sử dụng giao diện gốc của hệ điều hành theo mặc định. Tuy nhiên, bạn có thể thay đổi chủ đề của chúng để có giao diện trực quan tùy chỉnh, chẳng hạn như chế độ sáng và tối. Chủ đề là tập hợp các định nghĩa kiểu có thể tái sử dụng, mà bạn có thể coi là Biểu định kiểu xếp tầng (CSS) cho Tkinter

Làm cho các tiện ích mới có thể sử dụng được có nghĩa là trích xuất hầu hết thông tin kiểu dáng của chúng thành các đối tượng riêng biệt. Một mặt, sự phân tách các mối quan tâm như vậy là một thuộc tính mong muốn trong thiết kế của thư viện, nhưng mặt khác, nó giới thiệu một lớp trừu tượng bổ sung, làm cho các tiện ích theo chủ đề khó tạo kiểu hơn so với các tiện ích cổ điển

Khi làm việc với các widget thông thường và theo chủ đề trong Tkinter, theo thông lệ, bạn sẽ khai báo các bí danh sau cho các gói và mô-đun Tkinter

>>>

>>> import tkinter as tk
2

Các bí danh như thế này cho phép bạn đề cập rõ ràng đến

>>> import tkinter as tk
04 hoặc
>>> import tkinter as tk
26, chẳng hạn, trong một chương trình tùy thuộc vào nhu cầu của bạn

>>>

>>> import tkinter as tk
5

Tuy nhiên, đôi khi bạn có thể thấy thuận tiện hơn khi sử dụng nhập ký tự đại diện (

>>> import tkinter as tk
27) để tự động ghi đè tất cả các tiện ích cũ bằng các tiện ích theo chủ đề nếu có thể, như vậy

>>>

>>> import tkinter as tk
7

Bây giờ, bạn không cần phải thêm tiền tố vào tên lớp của tiện ích với mô-đun Python tương ứng của nó. Bạn sẽ luôn tạo một tiện ích theo chủ đề miễn là tiện ích đó có sẵn hoặc bạn sẽ quay lại tiện ích cổ điển nếu không. Hai câu lệnh nhập ở trên phải được đặt theo thứ tự đã chỉ định để có hiệu lực. Do đó, nhập ký tự đại diện được coi là một thông lệ xấu, thường nên tránh trừ khi được sử dụng một cách có ý thức

Để có danh sách đầy đủ các tiện ích Tkinter, hãy xem Tiện ích cơ bản và Tiện ích khác trong hướng dẫn TkDocs. Mặc dù nó mô tả các vật dụng theo chủ đề được giới thiệu trong Tcl/Tk 8. 5, hầu hết thông tin ở đó cũng nên áp dụng cho các vật dụng cổ điển

Sự thật thú vị. Tkinter nghĩa đen là viết tắt của “giao diện Tk” vì nó là một ràng buộc Python hoặc giao diện lập trình cho thư viện Tk bằng ngôn ngữ kịch bản Tcl

Hiện tại, hãy xem kỹ tiện ích

>>> import tkinter as tk
05

Loại bỏ các quảng cáo

Hiển thị văn bản và hình ảnh với các widget >>> import tkinter as tk 05

Tiện ích

>>> import tkinter as tk
05 được sử dụng để hiển thị văn bản hoặc hình ảnh. Người dùng không thể chỉnh sửa văn bản được hiển thị bởi tiện ích
>>> import tkinter as tk
05. Nó chỉ dành cho mục đích hiển thị. Như bạn đã thấy trong ví dụ ở phần đầu của hướng dẫn này, bạn có thể tạo tiện ích con
>>> import tkinter as tk
05 bằng cách khởi tạo lớp
>>> import tkinter as tk
05 và chuyển một chuỗi tới tham số
>>> import tkinter as tk
34

>>> import tkinter as tk
0

Tiện ích

>>> import tkinter as tk
05 hiển thị văn bản với màu văn bản hệ thống mặc định và màu nền văn bản hệ thống mặc định. Chúng thường có màu đen và trắng tương ứng, nhưng bạn có thể thấy các màu khác nếu bạn đã thay đổi các cài đặt này trong hệ điều hành của mình

Bạn có thể kiểm soát màu nền và văn bản của

>>> import tkinter as tk
05 bằng cách sử dụng các tham số
>>> import tkinter as tk
37 và
>>> import tkinter as tk
38

>>> import tkinter as tk
1

Có rất nhiều tên màu hợp lệ, bao gồm

  • >>> import tkinter as tk
    
    39
  • >>> import tkinter as tk
    
    40
  • >>> import tkinter as tk
    
    41
  • >>> import tkinter as tk
    
    42
  • >>> import tkinter as tk
    
    43
  • >>> import tkinter as tk
    
    44

Nhiều tên màu HTML hoạt động với Tkinter. Để tham khảo đầy đủ, bao gồm các màu hệ thống dành riêng cho macOS và Windows mà chủ đề hệ thống hiện tại kiểm soát, hãy xem trang hướng dẫn sử dụng màu

Bạn cũng có thể chỉ định màu bằng các giá trị RGB thập lục phân

>>> import tkinter as tk
2

Thao tác này đặt nền nhãn thành màu xanh lam nhạt đẹp mắt. Các giá trị RGB thập lục phân khó hiểu hơn các màu được đặt tên, nhưng chúng cũng linh hoạt hơn. May mắn thay, có những công cụ giúp việc lấy mã màu thập lục phân tương đối dễ dàng

Nếu bạn không muốn gõ

>>> import tkinter as tk
37 và
>>> import tkinter as tk
38 mọi lúc, thì bạn có thể sử dụng các tham số tốc ký
>>> import tkinter as tk
47 và
>>> import tkinter as tk
48 để đặt màu nền trước và nền sau

>>> import tkinter as tk
3

Bạn cũng có thể kiểm soát chiều rộng và chiều cao của nhãn bằng tham số

>>> import tkinter as tk
49 và
>>> import tkinter as tk
50

>>> import tkinter as tk
4

Đây là những gì nhãn này trông giống như trong một cửa sổ

Làm cách nào để mở tệp tkinter trong python?

Có vẻ lạ khi nhãn trong cửa sổ không phải là hình vuông mặc dù cả chiều rộng và chiều cao đều được đặt thành

>>> import tkinter as tk
51. Điều này là do chiều rộng và chiều cao được đo bằng đơn vị văn bản. Một đơn vị văn bản theo chiều ngang được xác định bởi chiều rộng của ký tự
>>> import tkinter as tk
52 hoặc số 0 trong phông chữ hệ thống mặc định. Tương tự, một đơn vị văn bản dọc được xác định bởi chiều cao của ký tự
>>> import tkinter as tk
52

Ghi chú. Đối với các phép đo chiều rộng và chiều cao, Tkinter sử dụng các đơn vị văn bản, thay vì đơn vị như inch, cm hoặc pixel, để đảm bảo hoạt động nhất quán của ứng dụng trên các nền tảng

Đơn vị đo lường theo chiều rộng của một ký tự có nghĩa là kích thước của tiện ích có liên quan đến phông chữ mặc định trên máy của người dùng. Điều này đảm bảo văn bản vừa vặn trong nhãn và nút, bất kể ứng dụng đang chạy ở đâu

Nhãn rất phù hợp để hiển thị một số văn bản, nhưng chúng không giúp bạn nhận thông tin đầu vào từ người dùng. Ba tiện ích tiếp theo mà bạn sẽ tìm hiểu đều được sử dụng để nhận đầu vào của người dùng

Loại bỏ các quảng cáo

Hiển thị các nút có thể nhấp với tiện ích con >>> import tkinter as tk 16

Các tiện ích

>>> import tkinter as tk
16 được sử dụng để hiển thị các nút có thể nhấp. Bạn có thể định cấu hình chúng để gọi một chức năng bất cứ khi nào chúng được nhấp vào. Bạn sẽ đề cập đến cách gọi các chức năng từ các lần nhấp vào nút trong phần tiếp theo. Bây giờ, hãy xem cách tạo và tạo kiểu cho một nút

Có nhiều điểm tương đồng giữa các widget

>>> import tkinter as tk
16 và
>>> import tkinter as tk
05. Theo nhiều cách, một nút chỉ là một nhãn mà bạn có thể nhấp vào. Các đối số từ khóa tương tự mà bạn sử dụng để tạo và tạo kiểu cho một
>>> import tkinter as tk
05 sẽ hoạt động với các tiện ích con của
>>> import tkinter as tk
16. Ví dụ: đoạn mã sau tạo một nút có nền màu xanh và văn bản màu vàng. Nó cũng đặt chiều rộng và chiều cao thành các đơn vị văn bản
>>> import tkinter as tk
60 và
>>> import tkinter as tk
61 tương ứng

>>> import tkinter as tk
5

Đây là giao diện của nút trong một cửa sổ

Làm cách nào để mở tệp tkinter trong python?

khá tiện lợi. Bạn có thể sử dụng hai tiện ích tiếp theo để thu thập văn bản nhập từ người dùng

Nhận đầu vào của người dùng với các widget >>> import tkinter as tk 17

Khi bạn cần lấy một chút văn bản từ người dùng, chẳng hạn như tên hoặc địa chỉ email, hãy sử dụng tiện ích

>>> import tkinter as tk
17. Nó sẽ hiển thị một hộp văn bản nhỏ mà người dùng có thể nhập một số văn bản vào. Tạo và tạo kiểu cho tiện ích con
>>> import tkinter as tk
17 hoạt động khá giống với tiện ích con
>>> import tkinter as tk
05 và
>>> import tkinter as tk
16. Ví dụ: đoạn mã sau tạo một tiện ích có nền màu xanh lam, một số văn bản màu vàng và chiều rộng là
>>> import tkinter as tk
67 đơn vị văn bản

>>> import tkinter as tk
6

Tuy nhiên, điều thú vị về các widget của

>>> import tkinter as tk
17 không phải là cách tạo kiểu cho chúng. Đó là cách sử dụng chúng để nhận thông tin đầu vào từ người dùng. Có ba thao tác chính mà bạn có thể thực hiện với các tiện ích
>>> import tkinter as tk
17

  1. Truy xuất văn bản bằng
    >>> import tkinter as tk
    
    70
  2. Xóa văn bản bằng
    >>> import tkinter as tk
    
    71
  3. Chèn văn bản với
    >>> import tkinter as tk
    
    72

Cách tốt nhất để hiểu về các vật dụng của

>>> import tkinter as tk
17 là tạo một vật dụng và tương tác với nó. Mở trình bao Python và làm theo các ví dụ trong phần này. Đầu tiên, nhập
>>> import tkinter as tk
20 và tạo một cửa sổ mới

>>>

>>> import tkinter as tk
7

Bây giờ hãy tạo tiện ích con

>>> import tkinter as tk
05 và
>>> import tkinter as tk
17

>>>

>>> import tkinter as tk
8

>>> import tkinter as tk
05 mô tả loại văn bản nào sẽ xuất hiện trong tiện ích
>>> import tkinter as tk
17. Nó không thực thi bất kỳ loại yêu cầu nào trên
>>> import tkinter as tk
17, nhưng nó cho người dùng biết chương trình của bạn muốn họ đặt gì ở đó. Bạn cần
>>> import tkinter as tk
10 các tiện ích vào cửa sổ để chúng hiển thị

>>>

>>> import tkinter as tk
9

Đây là những gì trông giống như

Làm cách nào để mở tệp tkinter trong python?

Lưu ý rằng Tkinter tự động căn giữa nhãn phía trên tiện ích

>>> import tkinter as tk
17 trong cửa sổ. Đây là một tính năng của
>>> import tkinter as tk
10, bạn sẽ tìm hiểu thêm về tính năng này trong các phần sau

Nhấp vào bên trong tiện ích

>>> import tkinter as tk
17 bằng chuột và nhập
>>> import tkinter as tk
84

Làm cách nào để mở tệp tkinter trong python?

Bây giờ bạn đã nhập một số văn bản vào tiện ích

>>> import tkinter as tk
17, nhưng văn bản đó chưa được gửi đến chương trình của bạn. Bạn có thể sử dụng
>>> import tkinter as tk
70 để truy xuất văn bản và gán nó cho một biến có tên là
>>> import tkinter as tk
87

>>>

>>> window = tk.Tk()
0

Bạn cũng có thể xóa văn bản. Phương thức

>>> import tkinter as tk
71 này nhận một đối số số nguyên cho Python biết ký tự nào cần xóa. Ví dụ: khối mã bên dưới cho biết cách
>>> import tkinter as tk
89 xóa ký tự đầu tiên khỏi
>>> import tkinter as tk
17

>>>

>>> window = tk.Tk()
1

Văn bản còn lại trong tiện ích hiện là

>>> import tkinter as tk
91

Làm cách nào để mở tệp tkinter trong python?

Lưu ý rằng, giống như đối tượng chuỗi Python, văn bản trong tiện ích con

>>> import tkinter as tk
17 được lập chỉ mục bắt đầu bằng
>>> import tkinter as tk
52

Nếu bạn cần xóa một số ký tự khỏi

>>> import tkinter as tk
17, sau đó chuyển đối số số nguyên thứ hai cho
>>> import tkinter as tk
71 cho biết chỉ mục của ký tự nơi việc xóa sẽ dừng lại. Ví dụ: đoạn mã sau xóa bốn chữ cái đầu tiên trong
>>> import tkinter as tk
17

>>>

>>> window = tk.Tk()
2

Văn bản còn lại bây giờ là

>>> import tkinter as tk
97

Làm cách nào để mở tệp tkinter trong python?

>>> import tkinter as tk
98 hoạt động giống như cắt chuỗi. Đối số đầu tiên xác định chỉ mục bắt đầu và quá trình xóa tiếp tục nhưng không bao gồm chỉ mục được chuyển làm đối số thứ hai. Sử dụng hằng số đặc biệt
>>> import tkinter as tk
99 cho đối số thứ hai của
>>> import tkinter as tk
71 để xóa tất cả văn bản trong
>>> import tkinter as tk
17

>>>

>>> window = tk.Tk()
3

Bây giờ bạn sẽ thấy một hộp văn bản trống

Làm cách nào để mở tệp tkinter trong python?

Ngược lại, bạn cũng có thể chèn văn bản vào tiện ích

>>> import tkinter as tk
17

>>>

>>> window = tk.Tk()
4

Cửa sổ bây giờ trông như thế này

Làm cách nào để mở tệp tkinter trong python?

Đối số đầu tiên cho biết

>>> import tkinter as tk
72 nơi chèn văn bản. Nếu không có văn bản nào trong
>>> import tkinter as tk
17 thì văn bản mới sẽ luôn được chèn vào đầu tiện ích con, bất kể bạn chuyển giá trị nào làm đối số đầu tiên. Ví dụ: gọi
>>> import tkinter as tk
72 với
>>> window = tk.Tk()
06 làm đối số đầu tiên thay vì
>>> import tkinter as tk
52, như bạn đã làm ở trên, sẽ tạo ra kết quả tương tự

Nếu

>>> import tkinter as tk
17 đã chứa một số văn bản, thì
>>> import tkinter as tk
72 sẽ chèn văn bản mới vào vị trí đã chỉ định và dịch chuyển tất cả văn bản hiện có sang bên phải

>>>

>>> window = tk.Tk()
5

Văn bản tiện ích hiện có nội dung là

>>> import tkinter as tk
84

Làm cách nào để mở tệp tkinter trong python?

Các tiện ích

>>> import tkinter as tk
17 rất phù hợp để thu thập một lượng nhỏ văn bản từ người dùng, nhưng vì chúng chỉ được hiển thị trên một dòng nên chúng không lý tưởng để thu thập một lượng lớn văn bản. Đó là nơi các vật dụng của
>>> import tkinter as tk
18 xuất hiện

Loại bỏ các quảng cáo

Nhận đầu vào của người dùng nhiều dòng với các widget >>> import tkinter as tk 18

Tiện ích

>>> import tkinter as tk
18 được sử dụng để nhập văn bản, giống như tiện ích
>>> import tkinter as tk
17. Điểm khác biệt là tiện ích
>>> import tkinter as tk
18 có thể chứa nhiều dòng văn bản. Với tiện ích
>>> import tkinter as tk
18, người dùng có thể nhập toàn bộ đoạn văn hoặc thậm chí nhiều trang văn bản. Cũng giống như với các tiện ích
>>> import tkinter as tk
17, bạn có thể thực hiện ba thao tác chính với các tiện ích
>>> import tkinter as tk
18

  1. Truy xuất văn bản với
    >>> import tkinter as tk
    
    70
  2. Xóa văn bản bằng
    >>> import tkinter as tk
    
    71
  3. Chèn văn bản với
    >>> import tkinter as tk
    
    72

Mặc dù tên phương thức giống như phương pháp

>>> import tkinter as tk
17, nhưng chúng hoạt động hơi khác một chút. Đã đến lúc bắt tay vào làm bằng cách tạo một tiện ích
>>> import tkinter as tk
18 và xem những gì nó có thể làm

Ghi chú. Bạn vẫn mở cửa sổ từ phần trước chứ?

Nếu vậy, thì bạn có thể đóng nó bằng cách thực hiện như sau

>>>

>>> window = tk.Tk()
6

Bạn cũng có thể đóng thủ công bằng cách nhấp vào nút Đóng

Trong trình bao Python của bạn, hãy tạo một cửa sổ trống mới và đóng gói tiện ích

>>> window = tk.Tk()
25 vào đó

>>>

>>> window = tk.Tk()
7

Theo mặc định, hộp văn bản lớn hơn nhiều so với tiện ích con

>>> import tkinter as tk
17. Đây là cửa sổ được tạo ở trên trông như thế nào

Làm cách nào để mở tệp tkinter trong python?

Nhấp vào bất kỳ đâu bên trong cửa sổ để kích hoạt hộp văn bản. Nhập từ

>>> window = tk.Tk()
27. Sau đó nhấn Enter và nhập
>>> window = tk.Tk()
28 trên dòng thứ hai. Cửa sổ bây giờ trông như thế này.

Làm cách nào để mở tệp tkinter trong python?

Giống như với tiện ích con

>>> import tkinter as tk
17, bạn có thể truy xuất văn bản từ tiện ích con
>>> import tkinter as tk
18 bằng cách sử dụng
>>> import tkinter as tk
70. Tuy nhiên, gọi
>>> import tkinter as tk
70 mà không có đối số sẽ không trả lại toàn bộ văn bản trong hộp văn bản giống như đối với tiện ích con
>>> import tkinter as tk
17. Nó đưa ra một ngoại lệ

>>>

>>> window = tk.Tk()
8

>>> window = tk.Tk()
34 yêu cầu ít nhất một đối số. Gọi
>>> import tkinter as tk
70 với một chỉ mục trả về một ký tự. Để truy xuất một số ký tự, bạn cần chuyển chỉ mục bắt đầu và chỉ mục kết thúc. Các chỉ số trong tiện ích con
>>> import tkinter as tk
18 hoạt động khác với trong tiện ích con
>>> import tkinter as tk
17. Vì tiện ích
>>> import tkinter as tk
18 có thể có nhiều dòng văn bản nên chỉ mục phải chứa hai phần thông tin

  1. The line number of a character
  2. Vị trí của một ký tự trên dòng đó

Số dòng bắt đầu bằng

>>> window = tk.Tk()
39 và vị trí ký tự bắt đầu bằng
>>> import tkinter as tk
52. Để tạo chỉ mục, bạn tạo một chuỗi có dạng
>>> window = tk.Tk()
41, thay thế
>>> window = tk.Tk()
42 bằng số dòng và
>>> window = tk.Tk()
43 bằng số ký tự. Ví dụ:
>>> window = tk.Tk()
44 đại diện cho ký tự đầu tiên trên dòng đầu tiên và
>>> window = tk.Tk()
45 đại diện cho ký tự thứ tư trên dòng thứ hai

Sử dụng chỉ mục

>>> window = tk.Tk()
44 để lấy chữ cái đầu tiên từ hộp văn bản mà bạn đã tạo trước đó

>>>

>>> window = tk.Tk()
9

Có năm chữ cái trong từ

>>> window = tk.Tk()
27 và số ký tự của
>>> window = tk.Tk()
48 là
>>> window = tk.Tk()
49, vì số ký tự bắt đầu từ
>>> import tkinter as tk
52 và từ
>>> window = tk.Tk()
27 bắt đầu ở vị trí đầu tiên trong hộp văn bản. Cũng giống như với Python string slice, để có được toàn bộ từ
>>> window = tk.Tk()
27 từ hộp văn bản, chỉ số kết thúc phải nhiều hơn một chỉ số của ký tự cuối cùng được đọc

Vì vậy, để lấy từ

>>> window = tk.Tk()
27 từ hộp văn bản, hãy sử dụng
>>> window = tk.Tk()
44 cho chỉ mục đầu tiên và
>>> window = tk.Tk()
55 cho chỉ mục thứ hai

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
10

Để có từ

>>> window = tk.Tk()
28 trên dòng thứ hai của hộp văn bản, hãy thay đổi số dòng trong mỗi chỉ mục thành
>>> window = tk.Tk()
57

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
11

Để lấy tất cả văn bản trong một hộp văn bản, hãy đặt chỉ mục bắt đầu bằng

>>> window = tk.Tk()
44 và sử dụng hằng số đặc biệt
>>> import tkinter as tk
99 cho chỉ mục thứ hai

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
12

Lưu ý rằng văn bản được trả về bởi

>>> import tkinter as tk
70 bao gồm mọi ký tự dòng mới. Bạn cũng có thể thấy từ ví dụ này rằng mọi dòng trong tiện ích
>>> import tkinter as tk
18 đều có ký tự xuống dòng ở cuối, bao gồm dòng văn bản cuối cùng trong hộp văn bản

>>> import tkinter as tk
71 được sử dụng để xóa các ký tự khỏi hộp văn bản. Nó hoạt động giống như
>>> import tkinter as tk
71 cho các vật dụng
>>> import tkinter as tk
17. Có hai cách để sử dụng
>>> import tkinter as tk
71

  1. Với một đối số duy nhất
  2. Với hai đối số

Sử dụng phiên bản một đối số, bạn chuyển tới

>>> import tkinter as tk
71 chỉ mục của một ký tự cần xóa. Ví dụ: sau đây xóa ký tự đầu tiên,
>>> window = tk.Tk()
67, khỏi hộp văn bản

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
13

Dòng văn bản đầu tiên trong cửa sổ hiện là

>>> window = tk.Tk()
68

Làm cách nào để mở tệp tkinter trong python?

Với phiên bản hai đối số, bạn chuyển hai chỉ mục để xóa một dải ký tự bắt đầu từ chỉ mục đầu tiên và tối đa nhưng không bao gồm chỉ mục thứ hai

For example, to delete the remaining

>>> window = tk.Tk()
68 on the first line of the text box, use the indices
>>> window = tk.Tk()
44 and
>>> window = tk.Tk()
71

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
14

Lưu ý rằng văn bản đã biến mất từ ​​​​dòng đầu tiên. Điều này để lại một dòng trống theo sau từ

>>> window = tk.Tk()
28 trên dòng thứ hai

Làm cách nào để mở tệp tkinter trong python?

Even though you can’t see it, there’s still a character on the first line. It’s a newline character. You can verify this using

>>> import tkinter as tk
70

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
15

If you delete that character, then the rest of the contents of the text box will shift up a line

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
13

Now,

>>> window = tk.Tk()
28 is on the first line of the text box

Làm cách nào để mở tệp tkinter trong python?

Try to clear out the rest of the text in the text box. Set

>>> window = tk.Tk()
44 as the start index and use
>>> import tkinter as tk
99 for the second index

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
17

The text box is now empty

Làm cách nào để mở tệp tkinter trong python?

You can insert text into a text box using

>>> import tkinter as tk
72

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
18

This inserts the word

>>> window = tk.Tk()
27 at the beginning of the text box, using the same
>>> window = tk.Tk()
79 format used by
>>> import tkinter as tk
70 to specify the insertion position

Làm cách nào để mở tệp tkinter trong python?

Check out what happens if you try to insert the word

>>> window = tk.Tk()
28 on the second line

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
19

Instead of inserting the text on the second line, the text is inserted at the end of the first line

Làm cách nào để mở tệp tkinter trong python?

If you want to insert text onto a new line, then you need to insert a newline character manually into the string being inserted

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
50

Now

>>> window = tk.Tk()
28 is on the second line of the text box

Làm cách nào để mở tệp tkinter trong python?

>>> import tkinter as tk
72 will do one of two things

  1. Insert text at the specified position if there’s already text at or after that position
  2. Append text to the specified line if the character number is greater than the index of the last character in the text box

It’s usually impractical to try and keep track of what the index of the last character is. The best way to insert text at the end of a

>>> import tkinter as tk
18 widget is to pass
>>> import tkinter as tk
99 to the first parameter of
>>> import tkinter as tk
72

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
51

Don’t forget to include the newline character (

>>> window = tk.Tk()
87) at the beginning of the text if you want to put it on a new line

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
52

>>> import tkinter as tk
05,
>>> import tkinter as tk
16,
>>> import tkinter as tk
17, and
>>> import tkinter as tk
18 widgets are just a few of the widgets available in Tkinter. There are several others, including widgets for checkboxes, radio buttons, scroll bars, and progress bars. For more information on all of the available widgets, see the Additional Widgets list in the Additional Resources section

Loại bỏ các quảng cáo

Assigning Widgets to Frames With >>> import tkinter as tk 19 Widgets

In this tutorial, you’re going to work with only five widgets

  1. >>> import tkinter as tk
    
    05
  2. >>> import tkinter as tk
    
    16
  3. >>> import tkinter as tk
    
    17
  4. >>> import tkinter as tk
    
    18
  5. >>> import tkinter as tk
    
    19

These are the four you’ve seen so far plus the

>>> import tkinter as tk
19 widget.
>>> import tkinter as tk
19 widgets are important for organizing the layout of your widgets in an application

Before you get into the details about laying out the visual presentation of your widgets, take a closer look at how

>>> import tkinter as tk
19 widgets work, and how you can assign other widgets to them. The following script creates a blank
>>> import tkinter as tk
19 widget and assigns it to the main application window

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
53

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
102 packs the frame into the window so that the window sizes itself as small as possible to encompass the frame. When you run the above script, you get some seriously uninteresting output

Làm cách nào để mở tệp tkinter trong python?

An empty

>>> import tkinter as tk
19 widget is practically invisible. Frames are best thought of as containers for other widgets. You can assign a widget to a frame by setting the widget’s
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
104 attribute

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
54

To get a feel for how this works, write a script that creates two

>>> import tkinter as tk
19 widgets called
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
106 and
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
107. In this script,
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
106 contains a label with the text
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
109, and
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
107 contains the label
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
111. Here’s one way to do this

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
55

Note that

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
106 is packed into the window before
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
107. The window that opens shows the label in
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
106 above the label in
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
107

Làm cách nào để mở tệp tkinter trong python?

Now see what happens when you swap the order of

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
116 and
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
117

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
56

The output looks like this

Làm cách nào để mở tệp tkinter trong python?

Now

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
118 is on top. Since
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
118 is assigned to
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
107, it moves to wherever
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
107 is positioned

All four of the widget types that you’ve learned about—

>>> import tkinter as tk
05,
>>> import tkinter as tk
16,
>>> import tkinter as tk
17, and
>>> import tkinter as tk
18—have a
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
104 attribute that’s set when you instantiate them. That way, you can control which
>>> import tkinter as tk
19 a widget is assigned to.
>>> import tkinter as tk
19 widgets are great for organizing other widgets in a logical manner. Related widgets can be assigned to the same frame so that, if the frame is ever moved in the window, then the related widgets stay together

Note. If you omit the

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
104 argument when creating a new widget instance, then it’ll be placed inside of the top-level window by default

In addition to grouping your widgets logically,

>>> import tkinter as tk
19 widgets can add a little flare to the visual presentation of your application. Read on to see how to create various borders for
>>> import tkinter as tk
19 widgets

Loại bỏ các quảng cáo

Adjusting Frame Appearance With Reliefs

>>> import tkinter as tk
19 widgets can be configured with a
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
133 attribute that creates a border around the frame. You can set
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
133 to be any of the following values

  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    135. Has no border effect (the default value)
  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    136. Creates a sunken effect
  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    137. Creates a raised effect
  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    138. Creates a grooved border effect
  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    139. Creates a ridged effect

To apply the border effect, you must set the

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
140 attribute to a value greater than
>>> window = tk.Tk()
39. This attribute adjusts the width of the border in pixels. The best way to get a feel for what each effect looks like is to see them for yourself. Here’s a script that packs five
>>> import tkinter as tk
19 widgets into a window, each with a different value for the
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
133 argument

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
57

Here’s a breakdown of this script

  • Lines 3 to 9 create a dictionary whose keys are the names of the different relief effects available in Tkinter. The values are the corresponding Tkinter objects. This dictionary is assigned to the

    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    144 variable

  • Line 13 starts a

    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    145 loop to loop over each item in the
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    144 dictionary

  • Line 14 creates a new

    >>> import tkinter as tk
    
    19 widget and assigns it to the
    >>> import tkinter as tk
    
    03 object. The
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    133 attribute is set to the corresponding relief in the
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    144 dictionary, and the
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    151 attribute is set to
    >>> import tkinter as tk
    
    61 so that the effect is visible

  • Line 15 packs the

    >>> import tkinter as tk
    
    19 into the window using
    >>> import tkinter as tk
    
    10. The
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    155 keyword argument tells Tkinter in which direction to pack the
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    156 objects. You’ll see more about how this works in the next section

  • Lines 16 and 17 create a

    >>> import tkinter as tk
    
    05 widget to display the name of the relief and pack it into the
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    156 object you just created

The window produced by the above script looks like this

Làm cách nào để mở tệp tkinter trong python?

In this image, you can see the following effects

  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    135 tạo khung có vẻ phẳng
  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    136 adds a border that gives the frame the appearance of being sunken into the window
  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    137 gives the frame a border that makes it appear to stick out from the screen
  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    138 adds a border that appears as a sunken groove around an otherwise flat frame
  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    139 gives the appearance of a raised lip around the edge of the frame

Những hiệu ứng này mang lại cho ứng dụng Python GUI Tkinter của bạn một chút hấp dẫn trực quan

Hiểu các quy ước đặt tên widget

Khi bạn tạo một tiện ích, bạn có thể đặt cho nó bất kỳ tên nào bạn thích, miễn là đó là mã định danh Python hợp lệ. Thông thường, bạn nên đưa tên của lớp tiện ích con vào tên biến mà bạn gán cho thể hiện tiện ích con. Ví dụ: nếu tiện ích con

>>> import tkinter as tk
05 được sử dụng để hiển thị tên người dùng, thì bạn có thể đặt tên cho tiện ích con đó là
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
165. Tiện ích
>>> import tkinter as tk
17 được sử dụng để thu thập tuổi của người dùng có thể được gọi là
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
167

Note. Sometimes, you may define a new widget without assigning it to a variable. You’ll call its

>>> import tkinter as tk
10 method directly on the same line of code

>>>

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
58

This might be helpful when you don’t intend to refer to the widget’s instance later on. Due to automatic memory management, Python would normally garbage collect such unassigned objects, but Tkinter prevents that by registering every new widget internally

When you include the widget class name in the variable name, you help yourself and anyone else who needs to read your code to understand what type of widget the variable name refers to. However, using the full name of the widget class can lead to long variable names, so you may want to adopt a shorthand for referring to each widget type. For the rest of this tutorial, you’ll use the following shorthand prefixes to name widgets

Widget ClassVariable Name PrefixExample

>>> import tkinter as tk
05
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
170
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
171
>>> import tkinter as tk
16
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
173
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
174
>>> import tkinter as tk
17
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
176
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
177
>>> import tkinter as tk
18
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
179
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
180
>>> import tkinter as tk
19
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
182
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
183

In this section, you learned how to create a window, use widgets, and work with frames. At this point, you can make some plain windows that display messages, but you’ve yet to create a full-blown application. In the next section, you’ll learn how to control the layout of your applications using Tkinter’s powerful geometry managers

Kiểm tra việc hiểu của bạn

Expand the code block below for an exercise to check your understanding

Tập thể dục. Create an Entry widget and insert some textShow/Hide

Write a complete script that displays an

>>> import tkinter as tk
17 widget that’s 40 text units wide and has a white background and black text. Sử dụng
>>> import tkinter as tk
72 để hiển thị văn bản trong tiện ích có nội dung
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
186

The output window should look like this

Làm cách nào để mở tệp tkinter trong python?

Hãy thử bài tập này ngay bây giờ

Bạn có thể mở rộng khối mã bên dưới để xem giải pháp

Giải pháp. Create an Entry widget and insert some textShow/Hide

There are a couple of ways to solve this exercise. Here’s one solution that uses the

>>> import tkinter as tk
48 and
>>> import tkinter as tk
47 parameters to set the
>>> import tkinter as tk
17 widget’s background and foreground colors

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
59

This solution is great because it explicitly sets the background and foreground colors for the

>>> import tkinter as tk
17 widget

On most systems, the default background color for an

>>> import tkinter as tk
17 widget is white, and the default foreground color is black. So, you might be able to generate the same window with the
>>> import tkinter as tk
48 and
>>> import tkinter as tk
47 parameters left out

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
60

Hãy nhớ rằng mã của bạn có thể trông khác

Khi bạn đã sẵn sàng, bạn có thể chuyển sang phần tiếp theo

Loại bỏ các quảng cáo

Kiểm soát bố cục với Trình quản lý hình học

Up until now, you’ve been adding widgets to windows and

>>> import tkinter as tk
19 widgets using
>>> import tkinter as tk
10, but you haven’t learned what exactly this method does. Let’s clear things up. Application layout in Tkinter is controlled with geometry managers. While
>>> import tkinter as tk
10 is an example of a geometry manager, it isn’t the only one. Tkinter has two others

  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    197
  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    198

Each window or

>>> import tkinter as tk
19 in your application can use only one geometry manager. However, different frames can use different geometry managers, even if they’re assigned to a frame or window using another geometry manager. Start by taking a closer look at
>>> import tkinter as tk
10

The >>> import tkinter as tk 10 Geometry Manager

The

>>> import tkinter as tk
10 geometry manager uses a packing algorithm to place widgets in a
>>> import tkinter as tk
19 or window in a specified order. For a given widget, the packing algorithm has two primary steps

  1. Compute a rectangular area called a parcel that’s just tall (or wide) enough to hold the widget and fills the remaining width (or height) in the window with blank space
  2. Center the widget in the parcel unless a different location is specified

>>> import tkinter as tk
10 is powerful, but it can be difficult to visualize. The best way to get a feel for
>>> import tkinter as tk
10 is to look at some examples. See what happens when you
>>> import tkinter as tk
10 three
>>> import tkinter as tk
05 widgets into a
>>> import tkinter as tk
19

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
61

>>> import tkinter as tk
10 places each
>>> import tkinter as tk
19 below the previous one by default, in the order that they’re assigned to the window

Làm cách nào để mở tệp tkinter trong python?

Each

>>> import tkinter as tk
19 is placed at the topmost available position. Therefore, the red
>>> import tkinter as tk
19 is placed at the top of the window. Then the yellow
>>> import tkinter as tk
19 is placed just below the red one and the blue
>>> import tkinter as tk
19 just below the yellow one

There are three invisible parcels, each containing one of the three

>>> import tkinter as tk
19 widgets. Each parcel is as wide as the window and as tall as the
>>> import tkinter as tk
19 that it contains. Because no anchor point was specified when
>>> import tkinter as tk
10 was called for each
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
518 they’re all centered inside of their parcels. That’s why each
>>> import tkinter as tk
19 is centered in the window

>>> import tkinter as tk
10 accepts some keyword arguments for more precisely configuring widget placement. For example, you can set the
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
521 keyword argument to specify in which direction the frames should fill. The options are
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
522 to fill in the horizontal direction,
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
523 to fill vertically, and
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
524 to fill in both directions. Here’s how you would stack the three frames so that each one fills the whole window horizontally

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
62

Notice that the

>>> import tkinter as tk
49 is not set on any of the
>>> import tkinter as tk
19 widgets.
>>> import tkinter as tk
49 is no longer necessary because each frame sets
>>> import tkinter as tk
10 to fill horizontally, overriding any width you may set

The window produced by this script looks like this

Làm cách nào để mở tệp tkinter trong python?

One of the nice things about filling the window with

>>> import tkinter as tk
10 is that the fill is responsive to window resizing. Try widening the window generated by the previous script to see how this works. As you widen the window, the width of the three
>>> import tkinter as tk
19 widgets grow to fill the window

Làm cách nào để mở tệp tkinter trong python?

Notice, though, that the

>>> import tkinter as tk
19 widgets don’t expand in the vertical direction

The

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
155 keyword argument of
>>> import tkinter as tk
10 specifies on which side of the window the widget should be placed. These are the available options

  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    534
  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    535
  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    536
  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    537

If you don’t set

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
155, then
>>> import tkinter as tk
10 will automatically use
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
534 and place new widgets at the top of the window, or at the topmost portion of the window that isn’t already occupied by a widget. For example, the following script places three frames side by side from left to right and expands each frame to fill the window vertically

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
63

This time, you have to specify the

>>> import tkinter as tk
50 keyword argument on at least one of the frames to force the window to have some height

The resulting window looks like this

Làm cách nào để mở tệp tkinter trong python?

Just like when you set

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
542 to make the frames responsive when you resized the window horizontally, you can set
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
543 to make the frames responsive when you resize the window vertically

Làm cách nào để mở tệp tkinter trong python?

Để làm cho bố cục thực sự đáp ứng, bạn có thể đặt kích thước ban đầu cho khung của mình bằng cách sử dụng thuộc tính

>>> import tkinter as tk
49 và
>>> import tkinter as tk
50. Then, set the
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
521 keyword argument of
>>> import tkinter as tk
10 to
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
524 and set the
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
549 keyword argument to
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
550

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
64

When you run the above script, you’ll see a window that initially looks the same as the one you generated in the previous example. The difference is that now you can resize the window however you want, and the frames will expand and fill the window responsively

Làm cách nào để mở tệp tkinter trong python?

Tuyệt đấy

Loại bỏ các quảng cáo

The import tkinter.filedialog tkinter.filedialog.asksaveasfilename()tkinter.filedialog.asksaveasfile()tkinter.filedialog.askopenfilename()tkinter.filedialog.askopenfile()tkinter.filedialog.askdirectory()tkinter.filedialog.askopenfilenames()tkinter.filedialog.askopenfiles()197 Geometry Manager

You can use

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
197 to control the precise location that a widget should occupy in a window or
>>> import tkinter as tk
19. You must provide two keyword arguments,
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
554 and
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
555, which specify the x- and y-coordinates for the top-left corner of the widget. Both
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
554 and
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
555 are measured in pixels, not text units

Keep in mind that the origin, where

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
554 and
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
555 are both
>>> import tkinter as tk
52, is the top-left corner of the
>>> import tkinter as tk
19 or window. Vì vậy, bạn có thể coi đối số
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
555 của
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
197 là số pixel từ trên cùng của cửa sổ và đối số
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
554 là số pixel từ cạnh trái của cửa sổ

Đây là một ví dụ về cách hoạt động của trình quản lý hình học

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
197

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
65

Đây là cách mã này hoạt động

  • Lines 5 and 6 create a new
    >>> import tkinter as tk
    
    19 widget called
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    156, measuring
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    568 pixels wide and
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    568 pixels tall, and pack it into the window with
    >>> import tkinter as tk
    
    10
  • Lines 8 and 9 create a new
    >>> import tkinter as tk
    
    05 called
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    572 with a red background and place it in
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    573 at position (0, 0)
  • Lines 11 and 12 create a second
    >>> import tkinter as tk
    
    05 called
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    575 with a yellow background and place it in
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    573 at position (75, 75)

Here’s the window that the code produces

Làm cách nào để mở tệp tkinter trong python?

Note that if you run this code on a different operating system that uses different font sizes and styles, then the second label might become partially obscured by the window’s edge. That’s why

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
197 isn’t used often. In addition to this, it has two main drawbacks

  1. Layout can be difficult to manage with
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    197. This is especially true if your application has lots of widgets
  2. Bố cục được tạo bằng
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    197 không phản hồi. They don’t change as the window is resized

One of the main challenges of cross-platform GUI development is making layouts that look good no matter which platform they’re viewed on, and

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
197 is a poor choice for making responsive and cross-platform layouts

Điều đó không có nghĩa là bạn không bao giờ nên sử dụng

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
197. In some cases, it might be just what you need. Ví dụ: nếu bạn đang tạo giao diện GUI cho bản đồ, thì
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
197 có thể là lựa chọn hoàn hảo để đảm bảo các tiện ích con được đặt ở khoảng cách chính xác với nhau trên bản đồ

>>> import tkinter as tk
10 is usually a better choice than
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
197, but even
>>> import tkinter as tk
10 has some downsides. The placement of widgets depends on the order in which
>>> import tkinter as tk
10 is called, so it can be difficult to modify existing applications without fully understanding the code controlling the layout. Trình quản lý hình học
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 giải quyết rất nhiều vấn đề này, như bạn sẽ thấy trong phần tiếp theo

Trình quản lý hình học import tkinter.filedialog tkinter.filedialog.asksaveasfilename()tkinter.filedialog.asksaveasfile()tkinter.filedialog.askopenfilename()tkinter.filedialog.askopenfile()tkinter.filedialog.askdirectory()tkinter.filedialog.askopenfilenames()tkinter.filedialog.askopenfiles()198

The geometry manager you’ll likely use most often is

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198, which provides all the power of
>>> import tkinter as tk
10 in a format that’s easier to understand and maintain

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 works by splitting a window or
>>> import tkinter as tk
19 into rows and columns. Bạn chỉ định vị trí của một tiện ích con bằng cách gọi
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 và chuyển các chỉ số hàng và cột cho các đối số từ khóa
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
594 và
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
595 tương ứng. Both row and column indices start at
>>> import tkinter as tk
52, so a row index of
>>> window = tk.Tk()
39 and a column index of
>>> window = tk.Tk()
57 tells
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 to place a widget in the third column of the second row

The following script creates a 3 × 3 grid of frames with

>>> import tkinter as tk
05 widgets packed into them

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
66

Here’s what the resulting window looks like

Làm cách nào để mở tệp tkinter trong python?

You’re using two geometry managers in this example. Each frame is attached to

>>> import tkinter as tk
03 with the
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 geometry manager

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
67

Each

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
603 is attached to its master
>>> import tkinter as tk
19 with
>>> import tkinter as tk
10

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
68

The important thing to realize here is that even though

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 is called on each
>>> import tkinter as tk
19 object, the geometry manager applies to the
>>> import tkinter as tk
03 object. Tương tự, bố cục của mỗi
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
156 được kiểm soát bằng trình quản lý hình học
>>> import tkinter as tk
10

The frames in the previous example are placed tightly next to one another. To add some space around each frame, you can set the padding of each cell in the grid. Padding is just some blank space that surrounds a widget and visually sets its content apart

The two types of padding are external and internal padding. External padding adds some space around the outside of a grid cell. It’s controlled with two keyword arguments to

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198

  1. import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    612 adds padding in the horizontal direction
  2. import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    613 thêm phần đệm theo hướng dọc

Both

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
612 and
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
613 are measured in pixels, not text units, so setting both of them to the same value will create the same amount of padding in both directions. Try to add some padding around the outside of the frames from the previous example

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
69

Here’s the resulting window

Làm cách nào để mở tệp tkinter trong python?

>>> import tkinter as tk
10 also has
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
612 and
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
613 parameters. Đoạn mã sau gần giống với đoạn mã trước, ngoại trừ việc bạn thêm năm pixel đệm bổ sung xung quanh mỗi nhãn theo cả hai hướng
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
554 và
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
555

$ sudo apt-get install python3-tk
10

Phần đệm bổ sung xung quanh các tiện ích

>>> import tkinter as tk
05 cung cấp cho mỗi ô trong lưới một chút khoảng trống giữa đường viền
>>> import tkinter as tk
19 và văn bản trong nhãn

Làm cách nào để mở tệp tkinter trong python?

Điều đó trông khá đẹp. But if you try and expand the window in any direction, then you’ll notice that the layout isn’t very responsive

Làm cách nào để mở tệp tkinter trong python?

The whole grid stays at the top-left corner as the window expands

By using

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
623 and
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
624 on the
>>> import tkinter as tk
03 object, you can adjust how the rows and columns of the grid grow as the window is resized. Hãy nhớ rằng, lưới được gắn vào
>>> import tkinter as tk
03, mặc dù bạn đang gọi
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 trên mỗi tiện ích
>>> import tkinter as tk
19. Cả
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
623 và
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
624 đều có ba đối số cơ bản

  1. Mục lục. The index of the grid column or row that you want to configure or a list of indices to configure multiple rows or columns at the same time
  2. Trọng lượng. A keyword argument called
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    631 that determines how the column or row should respond to window resizing, relative to the other columns and rows
  3. Kích cỡ nhỏ nhất. A keyword argument called
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    632 that sets the minimum size of the row height or column width in pixels

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
631 được đặt thành
>>> import tkinter as tk
52 theo mặc định, điều đó có nghĩa là cột hoặc hàng không mở rộng khi cửa sổ thay đổi kích thước. If every column or row is given a weight of
>>> window = tk.Tk()
39, then they all grow at the same rate. If one column has a weight of
>>> window = tk.Tk()
39 and another a weight of
>>> window = tk.Tk()
57, then the second column expands at twice the rate of the first. Adjust the previous script to better handle window resizing

$ sudo apt-get install python3-tk
11

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
623 và
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
624 được đặt trong phần thân của vòng lặp
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
145 bên ngoài. You could explicitly configure each column and row outside of the
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
145 loop, but that would require writing an additional six lines of code

On each iteration of the loop, the

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
642-th column and row are configured to have a weight of
>>> window = tk.Tk()
39. This ensures that the row and column expand at the same rate whenever the window is resized. Đối số
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
632 được đặt thành
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
645 cho mỗi cột và
>>> import tkinter as tk
67 cho mỗi hàng. This ensures that the
>>> import tkinter as tk
05 widget always displays its text without chopping off any characters, even if the window size is extremely small

The result is a grid layout that expands and contracts smoothly as the window is resized

Làm cách nào để mở tệp tkinter trong python?

Try it yourself to get a feel for how it works. Play around with the

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
631 and
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
632 parameters to see how they affect the grid

By default, widgets are centered in their grid cells. For example, the following code creates two

>>> import tkinter as tk
05 widgets and places them in a grid with one column and two rows

$ sudo apt-get install python3-tk
12

Each grid cell is

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
651 pixels wide and
>>> window = tk.Tk()
06 pixels tall. The labels are placed in the center of each cell, as you can see in the following figure

Làm cách nào để mở tệp tkinter trong python?

You can change the location of each label inside of the grid cell using the

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
653 parameter, which accepts a string containing one or more of the following letters

  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    654 hoặc
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    655 để căn chỉnh với phần trung tâm trên cùng của ô
  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    656 hoặc
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    657 để căn chỉnh về phía chính giữa bên phải của ô
  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    658 hoặc
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    659 để căn chỉnh với phần dưới cùng của ô
  • import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    660 hoặc
    import tkinter.filedialog

    tkinter.filedialog.asksaveasfilename()
    tkinter.filedialog.asksaveasfile()
    tkinter.filedialog.askopenfilename()
    tkinter.filedialog.askopenfile()
    tkinter.filedialog.askdirectory()
    tkinter.filedialog.askopenfilenames()
    tkinter.filedialog.askopenfiles()
    661 để căn chỉnh về phía chính giữa bên trái của ô

The letters

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
654,
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
658,
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
656, and
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
660 come from the cardinal directions north, south, east, and west. Setting
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
653 to
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
654 on both labels in the previous code positions each label at the top-center of its grid cell

$ sudo apt-get install python3-tk
13

Here’s the output

Làm cách nào để mở tệp tkinter trong python?

You can combine multiple letters in a single string to position each label in the corner of its grid cell

$ sudo apt-get install python3-tk
14

Trong ví dụ này, tham số

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
653 của
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
572 được đặt thành
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
670, đặt nhãn ở góc trên cùng bên phải của ô lưới của nó.
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
575 được định vị ở góc dưới cùng bên trái bằng cách chuyển
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
672 đến
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
653. Here’s what that looks like in the window

Làm cách nào để mở tệp tkinter trong python?

Khi một tiện ích được định vị bằng

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
653, kích thước của chính tiện ích đó chỉ đủ lớn để chứa bất kỳ văn bản nào và các nội dung khác bên trong tiện ích đó. It won’t fill the entire grid cell. Để điền vào lưới, bạn có thể chỉ định
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
675 để buộc tiện ích điền vào ô theo hướng dọc hoặc
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
676 để điền vào ô theo hướng ngang. Để điền vào toàn bộ ô, hãy đặt
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
653 thành
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
678. The following example illustrates each of these options

$ sudo apt-get install python3-tk
15

Here’s what the output looks like

Làm cách nào để mở tệp tkinter trong python?

Điều mà ví dụ trên minh họa là tham số

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
653 của trình quản lý hình học
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 có thể được sử dụng để đạt được các hiệu ứng tương tự như tham số
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
521 của trình quản lý hình học
>>> import tkinter as tk
10. Sự tương ứng giữa các tham số
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
653 và
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
521 được tóm tắt trong bảng sau

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198
>>> import tkinter as tk
10
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
687
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
543
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
689
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
542
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
691
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
692

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 là một trình quản lý hình học mạnh mẽ. Nó thường dễ hiểu hơn
>>> import tkinter as tk
10 và linh hoạt hơn nhiều so với
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
197. Khi tạo các ứng dụng Tkinter mới, bạn nên cân nhắc sử dụng
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 làm trình quản lý hình học chính của mình

Ghi chú.

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 offers much more flexibility than you’ve seen here. For example, you can configure cells to span multiple rows and columns. For more information, check out the Grid Geometry Manager section of the TkDocs tutorial

Giờ đây, bạn đã có kiến ​​thức cơ bản về trình quản lý hình học cho Tkinter khung GUI Python, bước tiếp theo là gán hành động cho các nút để đưa ứng dụng của bạn vào cuộc sống

Loại bỏ các quảng cáo

Kiểm tra việc hiểu của bạn

Expand the code block below for an exercise to check your understanding

Tập thể dục. Create an address entry formShow/Hide

Dưới đây là hình ảnh của một biểu mẫu nhập địa chỉ được thực hiện bằng Tkinter

Làm cách nào để mở tệp tkinter trong python?

Write a complete script that re-creates the window. Bạn có thể sử dụng bất kỳ trình quản lý hình học nào bạn thích

Bạn có thể mở rộng khối mã bên dưới để xem giải pháp

Giải pháp. Create an address entry formShow/Hide

Có nhiều cách khác nhau để giải bài tập này. Nếu giải pháp của bạn tạo ra một cửa sổ giống như cửa sổ trong câu lệnh bài tập, thì xin chúc mừng. Bạn đã giải thành công bài tập. Below, you can look at two solutions that use the

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 geometry manager

One solution creates a

>>> import tkinter as tk
05 and
>>> import tkinter as tk
17 widget with the desired settings for each field

$ sudo apt-get install python3-tk
16

There’s nothing wrong with this solution. It’s a bit long, but everything is very explicit. Nếu bạn muốn thay đổi điều gì đó, thì rõ ràng là bạn sẽ biết chính xác nơi cần thực hiện

That said, the solution can be considerably shortened by recognizing that each

>>> import tkinter as tk
17 has the same width, and that all you need for each
>>> import tkinter as tk
05 is the text

$ sudo apt-get install python3-tk
17

Trong giải pháp này, một danh sách được sử dụng để lưu trữ các chuỗi cho từng nhãn ở dạng. Chúng được lưu trữ theo thứ tự mà mỗi trường biểu mẫu sẽ xuất hiện. Sau đó,

$ sudo apt-get install python3-tk
103 nhận cả chỉ mục và chuỗi từ mỗi giá trị trong danh sách
$ sudo apt-get install python3-tk
104

Khi bạn đã sẵn sàng, bạn có thể chuyển sang phần tiếp theo

Making Your Applications Interactive

Đến bây giờ, bạn đã biết khá rõ về cách tạo cửa sổ với Tkinter, thêm một số widget và kiểm soát bố cục ứng dụng. That’s great, but applications shouldn’t just look good—they actually need to do something. In this section, you’ll learn how to bring your applications to life by performing actions whenever certain events occur

Using Events and Event Handlers

Khi bạn tạo một ứng dụng Tkinter, bạn phải gọi

>>> import tkinter as tk
11 để bắt đầu vòng lặp sự kiện. During the event loop, your application checks if an event has occurred. Nếu vậy, thì nó sẽ thực thi một số mã để đáp lại

The event loop is provided for you with Tkinter, so you don’t have to write any code that checks for events yourself. Tuy nhiên, bạn phải viết mã sẽ được thực thi để phản hồi lại một sự kiện. In Tkinter, you write functions called event handlers for the events that you use in your application

Ghi chú. Sự kiện là bất kỳ hành động nào xảy ra trong vòng lặp sự kiện có thể kích hoạt một số hành vi trong ứng dụng, chẳng hạn như khi nhấn phím hoặc nút chuột

When an event occurs, an event object is emitted, which means that an instance of a class representing the event is created. You don’t need to worry about instantiating these classes yourself. Tkinter sẽ tự động tạo các thể hiện của các lớp sự kiện cho bạn

You’ll write your own event loop in order to better understand how Tkinter’s event loop works. Bằng cách đó, bạn có thể thấy vòng lặp sự kiện của Tkinter phù hợp với ứng dụng của bạn như thế nào và phần nào bạn cần tự viết

Giả sử có một danh sách có tên là

$ sudo apt-get install python3-tk
106 chứa các đối tượng sự kiện. A new event object is automatically appended to
$ sudo apt-get install python3-tk
106 every time an event occurs in your program. You don’t need to implement this updating mechanism. It just automatically happens for you in this conceptual example. Using an infinite loop, you can continually check if there are any event objects in
$ sudo apt-get install python3-tk
106

$ sudo apt-get install python3-tk
18

Right now, the event loop that you’ve created doesn’t do anything with

$ sudo apt-get install python3-tk
109. Let’s change that. Giả sử ứng dụng của bạn cần phản hồi với các lần nhấn phím. Bạn cần kiểm tra xem
$ sudo apt-get install python3-tk
109 có được tạo bởi người dùng nhấn một phím trên bàn phím của họ hay không và nếu vậy, hãy chuyển
$ sudo apt-get install python3-tk
109 cho một chức năng xử lý sự kiện cho các lần nhấn phím

Giả sử rằng

$ sudo apt-get install python3-tk
109 có thuộc tính
$ sudo apt-get install python3-tk
113 được đặt thành chuỗi
$ sudo apt-get install python3-tk
114 nếu sự kiện là một đối tượng sự kiện nhấn phím và thuộc tính
$ sudo apt-get install python3-tk
115 chứa ký tự của phím được nhấn. Tạo một hàm
$ sudo apt-get install python3-tk
116 mới và cập nhật mã vòng lặp sự kiện của bạn

$ sudo apt-get install python3-tk
19

When you call

>>> import tkinter as tk
11, something like the above loop is run for you. This method takes care of two parts of the loop for you

  1. It maintains a list of events that have occurred
  2. Nó chạy một trình xử lý sự kiện bất cứ khi nào một sự kiện mới được thêm vào danh sách đó

Cập nhật vòng lặp sự kiện của bạn để sử dụng

>>> import tkinter as tk
11 thay vì vòng lặp sự kiện của riêng bạn

>>> import tkinter as tk
20

$ sudo apt-get install python3-tk
119 takes care of a lot for you, but there’s something missing from the above code. How does Tkinter know when to use
$ sudo apt-get install python3-tk
116? Tkinter widgets have a method called
$ sudo apt-get install python3-tk
121 for just this purpose

Loại bỏ các quảng cáo

Using $ sudo apt-get install python3-tk 121

Để gọi một trình xử lý sự kiện bất cứ khi nào một sự kiện xảy ra trên một tiện ích, hãy sử dụng

$ sudo apt-get install python3-tk
121. Trình xử lý sự kiện được cho là bị ràng buộc với sự kiện vì nó được gọi mỗi khi sự kiện xảy ra. You’ll continue with the keypress example from the previous section and use
$ sudo apt-get install python3-tk
121 to bind
$ sudo apt-get install python3-tk
116 to the keypress event

>>> import tkinter as tk
21

Ở đây, trình xử lý sự kiện

$ sudo apt-get install python3-tk
116 được liên kết với sự kiện
$ sudo apt-get install python3-tk
127 bằng cách sử dụng
$ sudo apt-get install python3-tk
128. Whenever a key is pressed while the application is running, your program will print the character of the key pressed

Ghi chú. The output of the above program is not printed in the Tkinter application window. It’s printed to the standard output stream (stdout)

Nếu bạn chạy chương trình ở chế độ IDLE, thì bạn sẽ thấy đầu ra trong cửa sổ tương tác. Nếu bạn chạy chương trình từ thiết bị đầu cuối, thì bạn sẽ thấy đầu ra trong thiết bị đầu cuối của mình

$ sudo apt-get install python3-tk
121 always takes at least two arguments

  1. Một sự kiện được đại diện bởi một chuỗi có dạng
    $ sudo apt-get install python3-tk
    
    130, trong đó
    $ sudo apt-get install python3-tk
    
    131 có thể là bất kỳ sự kiện nào của Tkinter
  2. Trình xử lý sự kiện là tên của hàm sẽ được gọi bất cứ khi nào sự kiện xảy ra

Trình xử lý sự kiện được liên kết với tiện ích mà trên đó

$ sudo apt-get install python3-tk
121 được gọi. Khi trình xử lý sự kiện được gọi, đối tượng sự kiện được chuyển đến hàm xử lý sự kiện

Trong ví dụ trên, trình xử lý sự kiện được liên kết với chính cửa sổ, nhưng bạn có thể liên kết trình xử lý sự kiện với bất kỳ tiện ích con nào trong ứng dụng của mình. For example, you can bind an event handler to a

>>> import tkinter as tk
16 widget that will perform some action whenever the button is pressed

>>> import tkinter as tk
22

Trong ví dụ này, sự kiện

$ sudo apt-get install python3-tk
134 trên tiện ích con
$ sudo apt-get install python3-tk
135 được liên kết với trình xử lý sự kiện
$ sudo apt-get install python3-tk
136. The
$ sudo apt-get install python3-tk
134 event occurs whenever the left mouse button is pressed while the mouse is over the widget. Có các sự kiện khác cho các lần nhấp vào nút chuột, bao gồm
$ sudo apt-get install python3-tk
138 cho nút chuột giữa và
$ sudo apt-get install python3-tk
139 cho nút chuột phải

Note. Để biết danh sách các sự kiện thường được sử dụng, hãy xem phần Loại sự kiện của Tkinter 8. 5 tài liệu tham khảo

Bạn có thể liên kết bất kỳ trình xử lý sự kiện nào với bất kỳ loại tiện ích nào bằng

$ sudo apt-get install python3-tk
121, nhưng có một cách đơn giản hơn để liên kết trình xử lý sự kiện với các lần nhấp vào nút bằng cách sử dụng thuộc tính
$ sudo apt-get install python3-tk
142 của tiện ích
>>> import tkinter as tk
16

Using $ sudo apt-get install python3-tk 142

Mỗi tiện ích

>>> import tkinter as tk
16 đều có thuộc tính
$ sudo apt-get install python3-tk
142 mà bạn có thể gán cho một chức năng. Whenever the button is pressed, the function is executed

Hãy xem một ví dụ. First, you’ll create a window with a

>>> import tkinter as tk
05 widget that holds a numeric value. Bạn sẽ đặt các nút ở bên trái và bên phải của nhãn. The left button will be used to decrease the value in the
>>> import tkinter as tk
05, and the right one will increase the value. Đây là mã cho cửa sổ

>>> import tkinter as tk
23

Cửa sổ trông như thế này

Làm cách nào để mở tệp tkinter trong python?

Với bố cục ứng dụng được xác định, bạn có thể làm cho nó trở nên sống động bằng cách đưa ra một số lệnh cho các nút. Bắt đầu với nút bên trái. Khi nhấn nút này, nó sẽ giảm giá trị trong nhãn xuống một. Để làm được điều này, trước tiên bạn cần có câu trả lời cho hai câu hỏi

  1. Làm thế nào để bạn có được văn bản trong
    >>> import tkinter as tk
    
    05?
  2. How do you update the text in
    >>> import tkinter as tk
    
    05?

Tiện ích

>>> import tkinter as tk
05 không có
>>> import tkinter as tk
70 như tiện ích
>>> import tkinter as tk
17 và
>>> import tkinter as tk
18 có. Tuy nhiên, bạn có thể truy xuất văn bản từ nhãn bằng cách truy cập thuộc tính
>>> import tkinter as tk
34 bằng ký hiệu chỉ số dưới kiểu từ điển

>>> import tkinter as tk
24

Bây giờ bạn đã biết cách lấy và đặt văn bản của nhãn, hãy viết hàm

$ sudo apt-get install python3-tk
155 để tăng giá trị trong
$ sudo apt-get install python3-tk
156 lên một

>>> import tkinter as tk
25

$ sudo apt-get install python3-tk
155 gets the text from
$ sudo apt-get install python3-tk
156 and converts it to an integer with
$ sudo apt-get install python3-tk
159. Sau đó, nó tăng giá trị này lên một và đặt thuộc tính
>>> import tkinter as tk
34 của nhãn thành giá trị mới này

Bạn cũng sẽ cần

$ sudo apt-get install python3-tk
161 để giảm giá trị trong
$ sudo apt-get install python3-tk
162 xuống một

>>> import tkinter as tk
26

Đặt

$ sudo apt-get install python3-tk
155 và
$ sudo apt-get install python3-tk
161 vào mã của bạn ngay sau câu lệnh
$ sudo apt-get install python3-tk
165

Để kết nối các nút với chức năng, hãy gán chức năng cho thuộc tính

$ sudo apt-get install python3-tk
142 của nút. You can do this when you instantiate the buttons. Ví dụ: cập nhật hai dòng khởi tạo các nút như sau

>>> import tkinter as tk
27

Đó là tất cả những gì bạn cần làm để liên kết các nút với

$ sudo apt-get install python3-tk
155 và
$ sudo apt-get install python3-tk
161 và làm cho chương trình hoạt động. Hãy thử lưu các thay đổi của bạn và chạy ứng dụng. Nhấp vào các nút để tăng và giảm giá trị ở giữa cửa sổ

Làm cách nào để mở tệp tkinter trong python?

Đây là mã ứng dụng đầy đủ để bạn tham khảo

Mã nguồn đầy đủ của ứng dụng truy cậpHiển thị/Ẩn

>>> import tkinter as tk
28

Ứng dụng này không đặc biệt hữu ích, nhưng những kỹ năng bạn học được ở đây áp dụng cho mọi ứng dụng bạn sẽ tạo

  • Use widgets to create the components of the user interface
  • Sử dụng trình quản lý hình học để kiểm soát bố cục của ứng dụng
  • Viết trình xử lý sự kiện tương tác với các thành phần khác nhau để nắm bắt và chuyển đổi đầu vào của người dùng

Trong hai phần tiếp theo, bạn sẽ xây dựng các ứng dụng hữu ích hơn. Đầu tiên, bạn sẽ xây dựng bộ chuyển đổi nhiệt độ để chuyển đổi giá trị nhiệt độ từ độ F sang độ C. Sau đó, bạn sẽ xây dựng một trình soạn thảo văn bản có thể mở, chỉnh sửa và lưu tệp văn bản

Kiểm tra việc hiểu của bạn

Expand the code block below for an exercise to check your understanding

Tập thể dục. Mô phỏng tung một con súc sắc sáu mặtHiện/Ẩn

Viết chương trình mô phỏng tung một con súc sắc sáu mặt. Nên có một nút với văn bản

$ sudo apt-get install python3-tk
169. When the user clicks the button, a random integer from
>>> window = tk.Tk()
39 to
$ sudo apt-get install python3-tk
171 should be displayed

Dấu. You can generate a random number using

$ sudo apt-get install python3-tk
172 in the
$ sudo apt-get install python3-tk
173 module. If you’re not familiar with the
$ sudo apt-get install python3-tk
173 module, then check out Generating Random Data in Python (Guide) for more information

Cửa sổ ứng dụng sẽ giống như thế này

Làm cách nào để mở tệp tkinter trong python?

Hãy thử bài tập này ngay bây giờ

Bạn có thể mở rộng khối mã bên dưới để xem giải pháp

Solution. Mô phỏng tung một con súc sắc sáu mặtHiện/Ẩn

Đây là một giải pháp khả thi

>>> import tkinter as tk
29

Hãy nhớ rằng mã của bạn có thể trông khác

Khi bạn đã sẵn sàng, bạn có thể chuyển sang phần tiếp theo

Building a Temperature Converter (Example App)

Trong phần này, bạn sẽ xây dựng một ứng dụng chuyển đổi nhiệt độ cho phép người dùng nhập nhiệt độ theo độ F và nhấn một nút để chuyển nhiệt độ đó sang độ C. Bạn sẽ xem qua mã từng bước. You can also find the full source code at the end of this section for your reference

Ghi chú. To get the most out of this section, follow along in a Python shell

Trước khi bạn bắt đầu viết mã, trước tiên bạn sẽ thiết kế ứng dụng. Bạn cần ba yếu tố

  1. >>> import tkinter as tk
    
    17. Một tiện ích có tên là
    $ sudo apt-get install python3-tk
    
    176 để nhập giá trị độ F
  2. >>> import tkinter as tk
    
    05. Một tiện ích có tên là
    $ sudo apt-get install python3-tk
    
    178 để hiển thị kết quả độ C
  3. >>> import tkinter as tk
    
    16. A widget called
    $ sudo apt-get install python3-tk
    
    180 that reads the value from the
    >>> import tkinter as tk
    
    17 widget, converts it from Fahrenheit to Celsius, and sets the text of the
    >>> import tkinter as tk
    
    05 widget to the result when clicked

Bạn có thể sắp xếp chúng trong một lưới với một hàng và một cột cho mỗi tiện ích. Điều đó mang lại cho bạn một ứng dụng hoạt động tối thiểu, nhưng nó không thân thiện với người dùng. Mọi thứ cần phải có nhãn

You’ll put a label directly to the right of the

$ sudo apt-get install python3-tk
176 widget containing the Fahrenheit symbol (℉) so that the user knows that the value
$ sudo apt-get install python3-tk
176 should be in degrees Fahrenheit. To do this, set the label text to
$ sudo apt-get install python3-tk
185, which uses Python’s named Unicode character support to display the Fahrenheit symbol

Bạn có thể tạo cho

$ sudo apt-get install python3-tk
180 một chút tinh tế bằng cách đặt văn bản của nó thành giá trị
$ sudo apt-get install python3-tk
187, giá trị này sẽ hiển thị một mũi tên màu đen chỉ sang phải. You’ll also make sure that
$ sudo apt-get install python3-tk
178 always has the Celsius symbol (℃) following the label text
$ sudo apt-get install python3-tk
189 to indicate that the result is in degrees Celsius. Đây là cửa sổ cuối cùng sẽ trông như thế nào

Làm cách nào để mở tệp tkinter trong python?

Bây giờ bạn đã biết mình cần những tiện ích nào và cửa sổ sẽ trông như thế nào, bạn có thể bắt đầu mã hóa nó. Đầu tiên, nhập

>>> import tkinter as tk
20 và tạo một cửa sổ mới

>>> import tkinter as tk
50

$ sudo apt-get install python3-tk
191 đặt tiêu đề của cửa sổ hiện có, trong khi
$ sudo apt-get install python3-tk
192 với cả hai đối số được đặt thành
$ sudo apt-get install python3-tk
193 làm cho cửa sổ có kích thước cố định. Cuối cùng khi bạn chạy ứng dụng này, cửa sổ sẽ có dòng chữ Bộ chuyển đổi nhiệt độ trên thanh tiêu đề của nó. Tiếp theo, tạo tiện ích con
$ sudo apt-get install python3-tk
176 có nhãn tên là
$ sudo apt-get install python3-tk
195 và gán cả hai cho tiện ích con
>>> import tkinter as tk
19 có tên là
$ sudo apt-get install python3-tk
197

>>> import tkinter as tk
51

Người dùng sẽ nhập giá trị Fahrenheit vào

$ sudo apt-get install python3-tk
176 và
$ sudo apt-get install python3-tk
195 được sử dụng để gắn nhãn cho
$ sudo apt-get install python3-tk
176 với ký hiệu Fahrenheit. Nhóm công-ten-nơ
$ sudo apt-get install python3-tk
197
$ sudo apt-get install python3-tk
176 và
$ sudo apt-get install python3-tk
195 cùng nhau

Bạn muốn đặt

$ sudo apt-get install python3-tk
195 ngay bên phải của
$ sudo apt-get install python3-tk
176. Bạn có thể bố trí chúng trong
$ sudo apt-get install python3-tk
197 bằng cách sử dụng trình quản lý hình học
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 với một hàng và hai cột

>>> import tkinter as tk
52

Bạn đã đặt tham số

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
653 thành
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
656 cho
$ sudo apt-get install python3-tk
176 để nó luôn dính vào cạnh ngoài cùng bên phải của ô lưới. Bạn cũng đặt
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
653 thành
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
660 cho
$ sudo apt-get install python3-tk
195 để giữ cho nó dính vào cạnh ngoài cùng bên trái của ô lưới. Điều này đảm bảo rằng
$ sudo apt-get install python3-tk
195 luôn nằm ngay bên phải của
$ sudo apt-get install python3-tk
176

Bây giờ, tạo

$ sudo apt-get install python3-tk
180 và
$ sudo apt-get install python3-tk
178 để chuyển đổi nhiệt độ đã nhập thành
$ sudo apt-get install python3-tk
176 và hiển thị kết quả

>>> import tkinter as tk
53

Like

$ sudo apt-get install python3-tk
197, both
$ sudo apt-get install python3-tk
180 and
$ sudo apt-get install python3-tk
178 are assigned to
>>> import tkinter as tk
03. Together, these three widgets make up the three cells in the main application grid. Sử dụng
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 để tiếp tục và bố trí chúng ngay bây giờ

>>> import tkinter as tk
54

Cuối cùng, chạy ứng dụng

>>> import tkinter as tk
55

Rằng sẽ rất tốt. Nhưng nút chưa làm gì cả. Ở đầu tệp tập lệnh của bạn, ngay bên dưới dòng

$ sudo apt-get install python3-tk
165, hãy thêm một hàm có tên là
>>> import tkinter as tk
225

>>> import tkinter as tk
56

Hàm này đọc giá trị từ

$ sudo apt-get install python3-tk
176, chuyển đổi nó từ độ F sang độ C, sau đó hiển thị kết quả ở dạng
$ sudo apt-get install python3-tk
178

Bây giờ đi xuống dòng nơi bạn xác định

$ sudo apt-get install python3-tk
180 và đặt tham số
$ sudo apt-get install python3-tk
142 của nó thành
>>> import tkinter as tk
230

>>> import tkinter as tk
57

Đó là nó. You’ve created a fully functional temperature converter app in just twenty-six lines of code. Khá mát mẻ, phải không?

Bạn có thể mở rộng khối mã bên dưới để xem toàn bộ tập lệnh

Mã nguồn đầy đủ của bộ chuyển đổi nhiệt độHiển thị/Ẩn

Đây là kịch bản đầy đủ để bạn tham khảo

>>> import tkinter as tk
58

Đã đến lúc đẩy mọi thứ lên một tầm cao mới. Đọc để tìm hiểu cách xây dựng trình soạn thảo văn bản

Building a Text Editor (Example App)

Trong phần này, bạn sẽ xây dựng một ứng dụng soạn thảo văn bản có thể tạo, mở, chỉnh sửa và lưu tệp văn bản. Có ba yếu tố cần thiết trong ứng dụng

  1. Tiện ích
    >>> import tkinter as tk
    
    16 có tên là
    >>> import tkinter as tk
    
    232 để mở tệp để chỉnh sửa
  2. Tiện ích
    >>> import tkinter as tk
    
    16 có tên là
    >>> import tkinter as tk
    
    234 để lưu tệp
  3. Tiện ích
    >>> import tkinter as tk
    
    235 có tên là
    >>> import tkinter as tk
    
    236 để tạo và chỉnh sửa tệp văn bản

Ba tiện ích sẽ được sắp xếp sao cho hai nút ở bên trái cửa sổ và hộp văn bản ở bên phải. Toàn bộ cửa sổ phải có chiều cao tối thiểu là 800 pixel và

>>> import tkinter as tk
236 phải có chiều rộng tối thiểu là 800 pixel. Toàn bộ bố cục phải đáp ứng để nếu cửa sổ được thay đổi kích thước, thì
>>> import tkinter as tk
236 cũng được thay đổi kích thước. Tuy nhiên, chiều rộng của khung chứa các nút không được thay đổi

Đây là một bản phác thảo về giao diện của cửa sổ

Làm cách nào để mở tệp tkinter trong python?

Bạn có thể đạt được bố cục mong muốn bằng trình quản lý hình học

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198. Bố cục chứa một hàng và hai cột

  1. Một cột hẹp bên trái dành cho các nút
  2. Một cột rộng hơn ở bên phải cho hộp văn bản

Để đặt kích thước tối thiểu cho cửa sổ và

>>> import tkinter as tk
236, bạn có thể đặt tham số
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
632 của phương thức cửa sổ
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
624 và
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
623 thành
>>> import tkinter as tk
244. Để xử lý thay đổi kích thước, bạn có thể đặt tham số
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
631 của các phương thức này thành
>>> window = tk.Tk()
39

Để đưa cả hai nút vào cùng một cột, bạn cần tạo tiện ích con

>>> import tkinter as tk
19 có tên là
>>> import tkinter as tk
248. Theo bản phác thảo, hai nút phải được xếp chồng lên nhau theo chiều dọc bên trong khung này, với
>>> import tkinter as tk
232 ở trên cùng. Bạn có thể làm điều đó với trình quản lý hình học
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 hoặc
>>> import tkinter as tk
10. Hiện tại, bạn sẽ gắn bó với
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198 vì làm việc với nó dễ dàng hơn một chút

Bây giờ bạn đã có kế hoạch, bạn có thể bắt đầu mã hóa ứng dụng. Bước đầu tiên là tạo tất cả các vật dụng bạn cần

>>> import tkinter as tk
59

Đây là một sự cố của mã này

  • Dòng 1 nhập khẩu
    >>> import tkinter as tk
    
    20
  • Dòng 3 và 4 tạo một cửa sổ mới với tiêu đề
    >>> import tkinter as tk
    
    254
  • Dòng 6 và 7 đặt cấu hình hàng và cột
  • Các dòng 9 đến 12 tạo bốn tiện ích mà bạn sẽ cần cho hộp văn bản, khung và các nút mở và lưu

Hãy nhìn vào dòng 6 kỹ hơn. Tham số

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
632 của
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
624 được đặt thành
>>> import tkinter as tk
244 và
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
631 được đặt thành
>>> window = tk.Tk()
39

>>> import tkinter as tk
70

Đối số đầu tiên là ________ 352, đặt chiều cao của hàng đầu tiên thành ________ 3244 pixel và đảm bảo rằng chiều cao của hàng tăng tỷ lệ thuận với chiều cao của cửa sổ. Chỉ có một hàng trong bố cục ứng dụng, vì vậy các cài đặt này áp dụng cho toàn bộ cửa sổ

Hãy cũng xem xét kỹ hơn dòng 7. Tại đây, bạn sử dụng

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
623 để đặt thuộc tính
>>> import tkinter as tk
49 và
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
631 của cột có chỉ số lần lượt là
>>> window = tk.Tk()
39 thành
>>> import tkinter as tk
244 và
>>> window = tk.Tk()
39

>>> import tkinter as tk
71

Remember, row and column indices are zero-based, so these settings apply only to the second column. Bằng cách chỉ định cấu hình cột thứ hai, hộp văn bản sẽ mở rộng và co lại một cách tự nhiên khi cửa sổ được thay đổi kích thước, trong khi cột chứa các nút sẽ giữ nguyên chiều rộng cố định

Bây giờ bạn có thể làm việc trên bố cục ứng dụng. Đầu tiên, gán hai nút cho khung

>>> import tkinter as tk
248 bằng trình quản lý hình học
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
198

>>> import tkinter as tk
72

Hai dòng mã này tạo một lưới có hai hàng và một cột trong khung

>>> import tkinter as tk
248 vì cả
>>> import tkinter as tk
232 và
>>> import tkinter as tk
234 đều có thuộc tính
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
104 được đặt thành
>>> import tkinter as tk
248.
>>> import tkinter as tk
232 được đặt ở hàng đầu tiên và
>>> import tkinter as tk
234 ở hàng thứ hai để
>>> import tkinter as tk
232 xuất hiện phía trên
>>> import tkinter as tk
234 trong bố cục, bạn chỉ định trong bản phác thảo của mình

Cả

>>> import tkinter as tk
232 và
>>> import tkinter as tk
234 đều có thuộc tính
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
653 được đặt thành
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
676, điều này buộc các nút mở rộng theo chiều ngang theo cả hai hướng và lấp đầy toàn bộ khung. Điều này đảm bảo rằng cả hai nút đều có cùng kích thước

Bạn đặt năm pixel đệm xung quanh mỗi nút bằng cách đặt tham số

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
612 và
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
613 thành
>>> import tkinter as tk
61. Chỉ
>>> import tkinter as tk
232 có đệm dọc. Vì nó ở trên cùng, nên phần đệm dọc sẽ dịch chuyển nút xuống từ trên cùng của cửa sổ một chút và đảm bảo rằng có một khoảng cách nhỏ giữa nó và
>>> import tkinter as tk
234

Bây giờ,

>>> import tkinter as tk
248 đã được bố trí và sẵn sàng hoạt động, bạn có thể thiết lập bố cục dạng lưới cho phần còn lại của cửa sổ

>>> import tkinter as tk
73

Hai dòng mã này tạo lưới có một hàng và hai cột cho

>>> import tkinter as tk
03. Bạn đặt
>>> import tkinter as tk
248 vào cột đầu tiên và
>>> import tkinter as tk
236 vào cột thứ hai để
>>> import tkinter as tk
248 xuất hiện ở bên trái của
>>> import tkinter as tk
236 trong bố cục cửa sổ

Tham số

import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
653 cho
>>> import tkinter as tk
248 được đặt thành
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
675, buộc toàn bộ khung mở rộng theo chiều dọc và lấp đầy toàn bộ chiều cao của cột của nó.
>>> import tkinter as tk
236 lấp đầy toàn bộ ô lưới của nó vì bạn đặt tham số
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
653 của nó thành
import tkinter.filedialog

tkinter.filedialog.asksaveasfilename()
tkinter.filedialog.asksaveasfile()
tkinter.filedialog.askopenfilename()
tkinter.filedialog.askopenfile()
tkinter.filedialog.askdirectory()
tkinter.filedialog.askopenfilenames()
tkinter.filedialog.askopenfiles()
678, điều này buộc nó phải mở rộng theo mọi hướng

Now that the application layout is complete, add

>>> import tkinter as tk
11 to the bottom of the program and save and run the file

>>> import tkinter as tk
74

Cửa sổ sau được hiển thị

Làm cách nào để mở tệp tkinter trong python?

That looks great. Nhưng nó chưa làm gì cả, vì vậy bạn cần bắt đầu viết lệnh cho các nút.

>>> import tkinter as tk
232 cần hiển thị hộp thoại mở tệp và cho phép người dùng chọn tệp. Sau đó, nó cần mở tệp đó và đặt văn bản của
>>> import tkinter as tk
236 thành nội dung của tệp. Đây là một hàm
>>> import tkinter as tk
503 thực hiện điều này

>>> import tkinter as tk
75

Đây là một sự cố của chức năng này

  • Các dòng 5 đến 7 sử dụng hộp thoại
    >>> import tkinter as tk
    
    504 từ mô-đun
    >>> import tkinter as tk
    
    505 để hiển thị hộp thoại mở tệp và lưu trữ đường dẫn tệp đã chọn tới
    >>> import tkinter as tk
    
    506
  • Dòng 8 và 9 kiểm tra xem người dùng có đóng hộp thoại hay nhấp vào nút Hủy không. If so, then
    >>> import tkinter as tk
    
    506 will be
    >>> import tkinter as tk
    
    508, and the function will
    >>> import tkinter as tk
    
    509 without executing any of the code to read the file and set the text of
    >>> import tkinter as tk
    
    236
  • Dòng 10 xóa nội dung hiện tại của
    >>> import tkinter as tk
    
    236 bằng cách sử dụng
    >>> import tkinter as tk
    
    71
  • Dòng 11 và 12 mở tệp đã chọn và
    >>> import tkinter as tk
    
    513 nội dung của nó trước khi lưu trữ
    >>> import tkinter as tk
    
    34 dưới dạng chuỗi
  • Dòng 13 gán chuỗi
    >>> import tkinter as tk
    
    34 cho
    >>> import tkinter as tk
    
    236 bằng cách sử dụng
    >>> import tkinter as tk
    
    72
  • Dòng 14 đặt tiêu đề của cửa sổ để nó chứa đường dẫn của tệp đang mở

Bây giờ bạn có thể cập nhật chương trình để

>>> import tkinter as tk
232 gọi
>>> import tkinter as tk
503 bất cứ khi nào nó được nhấp vào. Có một số điều bạn cần làm để cập nhật chương trình. Đầu tiên, nhập
>>> import tkinter as tk
504 từ
>>> import tkinter as tk
505 bằng cách thêm phần nhập sau vào đầu chương trình của bạn

>>> import tkinter as tk
76

Tiếp theo, đặt thuộc tính

$ sudo apt-get install python3-tk
142 của
>>> import tkinter as tk
523 thành
>>> import tkinter as tk
524

>>> import tkinter as tk
77

Lưu tệp và chạy nó để kiểm tra xem mọi thứ có hoạt động không. Sau đó thử mở một tệp văn bản

Khi

>>> import tkinter as tk
232 đang hoạt động, đã đến lúc thực hiện chức năng cho
>>> import tkinter as tk
234. This needs to open a save file dialog box so that the user can choose where they would like to save the file. You’ll use the
>>> import tkinter as tk
527 dialog in the
>>> import tkinter as tk
505 module for this. This function also needs to extract the text currently in
>>> import tkinter as tk
236 and write this to a file at the selected location. Đây là một chức năng chỉ làm điều này

>>> import tkinter as tk
78

Đây là cách mã này hoạt động

  • Dòng 19 đến 22 sử dụng hộp thoại
    >>> import tkinter as tk
    
    527 để lấy vị trí lưu mong muốn từ người dùng. The selected file path is stored in the
    >>> import tkinter as tk
    
    506 variable
  • Dòng 23 và 24 kiểm tra xem người dùng có đóng hộp thoại hay nhấp vào nút Hủy không. Nếu vậy, thì
    >>> import tkinter as tk
    
    506 sẽ là
    >>> import tkinter as tk
    
    508 và hàm sẽ trả về mà không thực thi bất kỳ mã nào để lưu văn bản vào một tệp
  • Dòng 25 tạo một tệp mới tại đường dẫn tệp đã chọn
  • Line 26 extracts the text from
    >>> import tkinter as tk
    
    236 with
    >>> import tkinter as tk
    
    70 method and assigns it to the variable
    >>> import tkinter as tk
    
    34
  • Line 27 writes
    >>> import tkinter as tk
    
    34 to the output file
  • Line 28 updates the title of the window so that the new file path is displayed in the window title

Now you can update the program so that

>>> import tkinter as tk
234 calls
>>> import tkinter as tk
539 when it’s clicked. Again, there are a few things you need to do in order to update the program. First, import
>>> import tkinter as tk
527 from
>>> import tkinter as tk
505 by updating the import at the top of your script, like so

>>> import tkinter as tk
79

Cuối cùng, đặt thuộc tính

$ sudo apt-get install python3-tk
142 của
>>> import tkinter as tk
234 thành
>>> import tkinter as tk
544

>>> import tkinter as tk
00

Lưu tệp và chạy nó. You’ve now got a minimal yet fully functional text editor

Bạn có thể mở rộng khối mã bên dưới để xem toàn bộ tập lệnh

Mã nguồn đầy đủ của ứng dụng soạn thảo văn bảnHiển thị/Ẩn

Đây là kịch bản đầy đủ để bạn tham khảo

>>> import tkinter as tk
01

Bây giờ bạn đã xây dựng hai ứng dụng GUI bằng Python và áp dụng nhiều kỹ năng mà bạn đã học được trong hướng dẫn này. Đó không phải là thành tựu nhỏ, vì vậy hãy dành thời gian để cảm thấy hài lòng về những gì bạn đã làm. You’re now ready to tackle some applications on your own

Sự kết luận

In this tutorial, you learned how to get started with Python GUI programming. Tkinter là một lựa chọn hấp dẫn cho khung GUI Python vì nó được tích hợp trong thư viện chuẩn Python và việc tạo các ứng dụng với khung này tương đối dễ dàng

Throughout this tutorial, you’ve learned several important Tkinter concepts

  • How to work with widgets
  • How to control your application layout with geometry managers
  • Cách làm cho ứng dụng của bạn tương tác
  • How to use five basic Tkinter widgets.
    >>> import tkinter as tk
    
    05,
    >>> import tkinter as tk
    
    16,
    >>> import tkinter as tk
    
    17,
    >>> import tkinter as tk
    
    18, and
    >>> import tkinter as tk
    
    19

Now that you’ve mastered the foundations of Python GUI programming with Tkinter, the next step is to build some of your own applications. What will you create? Share your fun projects down in the comments below

Additional Resources

In this tutorial, you touched on just the foundations of creating Python GUI applications with Tkinter. There are a number of additional topics that aren’t covered here. In this section, you’ll find some of the best resources available to help you continue on your journey

Tkinter References

Here are some official resources to check out

  • The official Python Tkinter reference documentation covers Python’s Tkinter module in moderate depth. It’s written for more advanced Python developers and isn’t the best resource for beginners
  • Tkinter 8. 5 reference. GUI cho Python là một tài liệu tham khảo mở rộng bao gồm phần lớn mô-đun Tkinter. It’s exhaustive, but it’s written in the reference style without commentary or examples
  • The Tk Commands reference is the definitive guide to commands in the Tk library. It’s written for the Tcl language, but it answers a lot of questions about why things work the way they do in Tkinter

Additional Widgets

In this tutorial, you learned about the

>>> import tkinter as tk
05,
>>> import tkinter as tk
16,
>>> import tkinter as tk
17,
>>> import tkinter as tk
18, and
>>> import tkinter as tk
19 widgets. There are several other widgets in Tkinter, all of which are essential for building real-world applications. Here are some resources to continue learning about widgets

  • The TkDocs Tkinter Tutorial is a fairly comprehensive guide for Tk, the underlying code library used by Tkinter. Examples are presented in Python, Ruby, Perl, and Tcl. You can find several examples of widgets beyond those covered here in two sections
    • Basic Widgets covers the same widgets as this tutorial, plus a few more
    • More Widgets covers several additional widgets
  • The official Python docs cover additional widgets
    • ttk themed widgets covers the Tk themed widget set
    • Scrolled Text Widget details a
      >>> import tkinter as tk
      
      18 widget combined with a vertical scroll bar

Application Distribution

Once you’ve created an application with Tkinter, you probably want to distribute it to your colleagues and friends. Here are some tutorials to get you going with that process

  • Using PyInstaller to Easily Distribute Python Applications
  • 4 Attempts at Packaging Python as an Executable
  • Building Standalone Python Applications with PyOxidizer

Other GUI Frameworks

Tkinter không phải là lựa chọn duy nhất của bạn cho khung GUI Python. Nếu Tkinter không đáp ứng nhu cầu của dự án của bạn, thì đây là một số khuôn khổ khác để xem xét

  • Cách xây dựng ứng dụng GUI Python với wxPython
  • Python và PyQt. Xây dựng Máy tính để bàn GUI
  • Xây dựng ứng dụng di động với Kivy Python Framework
  • PySimpleGUI. Cách đơn giản để tạo GUI bằng Python

Lấy bài kiểm tra. Kiểm tra kiến ​​thức của bạn với bài kiểm tra tương tác “Lập trình GUI Python với Tkinter” của chúng tôi. Sau khi hoàn thành, bạn sẽ nhận được điểm số để có thể theo dõi quá trình học tập của mình theo thời gian

Lấy bài kiểm tra "

Đánh dấu là đã hoàn thành

🐍 Thủ thuật Python 💌

Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python

Làm cách nào để mở tệp tkinter trong python?

Gửi cho tôi thủ thuật Python »

Giới thiệu về David Amos

Làm cách nào để mở tệp tkinter trong python?
Làm cách nào để mở tệp tkinter trong python?

David là một nhà văn, lập trình viên và nhà toán học đam mê khám phá toán học thông qua mã

» Thông tin thêm về David


Mỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. The team members who worked on this tutorial are

Làm cách nào để mở tệp tkinter trong python?

Aldren

Làm cách nào để mở tệp tkinter trong python?

Bartosz

Làm cách nào để mở tệp tkinter trong python?

Geir Arne

Làm cách nào để mở tệp tkinter trong python?

Jaya

Làm cách nào để mở tệp tkinter trong python?

Joanna

Làm cách nào để mở tệp tkinter trong python?

kate

Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực

Làm cách nào để mở tệp tkinter trong python?

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas

Nâng cao kỹ năng Python của bạn »

Bậc thầy Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bạn nghĩ sao?

Đánh giá bài viết này

Tweet Chia sẻ Chia sẻ Email

Bài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?

Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. Nhận các mẹo để đặt câu hỏi hay và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi