Skip to content

Commit 991127b

Browse files
authored
Update README.md
1 parent 7929cd9 commit 991127b

File tree

1 file changed

+159
-1
lines changed

1 file changed

+159
-1
lines changed

README.md

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,159 @@
1-
# Comprehensive-Tkinter-Cheatsheet
1+
# Comprehensive Tkinter Cheatsheet 📃🏷
2+
![Header](https://github.com/shabir-mp/Comprehensive-Tkinter-Cheatsheet/assets/133546000/74af8f4f-7cca-4cc3-a2af-4fc74cff4b03)
3+
source : real python
4+
5+
## Introduction to Tkinter
6+
Tkinter is a built-in Python module used to create graphical user interfaces (GUIs). It serves as a wrapper between Python and the Tk GUI toolkit created by Tcl/Tk. Tkinter provides various GUI widgets such as buttons, labels, text boxes, check boxes, and more, allowing users to create applications with interactive interfaces.
7+
8+
One of the advantages of Tkinter is that it comes as a standard module in the Python distribution, so there's no need to install additional packages to use it. Additionally, Tkinter is relatively easy to learn for beginners, as it has good documentation and many tutorials available.
9+
10+
The process of creating an interface using Tkinter involves creating widget objects and configuring their properties, such as size, position, and layout. After that, users can add business logic to the application, such as responding to user interactions with widgets or manipulating data.
11+
12+
Overall, Tkinter is a great choice for creating applications with graphical interfaces in Python, especially for small to medium-sized projects. However, for larger and more complex projects, some developers may opt for other GUI toolkits that are more powerful and flexible.
13+
14+
## History of Tkinter
15+
Tkinter has a rich history within the Python programming language. Here's a brief overview:
16+
17+
1. **Early Development (Late 1980s - Early 1990s):** Tkinter's story begins with the creation of the Tcl (Tool Command Language) scripting language by John Ousterhout in the late 1980s. Tcl came with a graphical toolkit called Tk (Toolkit), which was developed by Ousterhout and his team at the University of California, Berkeley. Tk provided a set of graphical user interface widgets that could be used to build GUI applications.
18+
19+
2. **Integration with Python (Early 1990s):** In the early 1990s, Guido van Rossum, the creator of Python, recognized the potential of Tk for Python's graphical capabilities. He collaborated with others to integrate Tk into Python, creating Tkinter as the standard GUI toolkit for Python. Tkinter combined the simplicity and elegance of Python with the power and flexibility of Tk.
20+
21+
3. **Maturation and Standardization (1990s - 2000s):** Tkinter became an essential part of Python's standard library, ensuring its availability and consistency across different Python installations. Over the years, Tkinter continued to evolve, with improvements in performance, features, and compatibility.
22+
23+
4. **Continued Development (2010s - Present):** Tkinter remained a popular choice for Python developers due to its simplicity, ease of use, and cross-platform compatibility. Despite the emergence of alternative GUI toolkits and frameworks, Tkinter maintained its relevance and continued to be widely used in both small-scale and large-scale Python projects.
24+
25+
5. **Modernization Efforts (2010s - Present):** In recent years, efforts have been made to modernize Tkinter and enhance its capabilities. This includes updates to support newer versions of Python, improvements in documentation and tutorials, and community-driven initiatives to address usability issues and add new features.
26+
27+
Overall, Tkinter has a long and storied history as Python's standard GUI toolkit, playing a crucial role in enabling developers to create graphical applications with Python. Its simplicity, reliability, and integration with Python have made it a popular choice for developers of all levels of experience.
28+
29+
-------------------------------------
30+
## 1. Importing Tkinter
31+
```python
32+
import tkinter as tk
33+
```
34+
Tkinter is usually imported using the `import tkinter as tk` statement. This allows us to access Tkinter classes and functions using the `tk` namespace.
35+
36+
## 2. Creating the Main Window
37+
```python
38+
root = tk.Tk()
39+
```
40+
The `Tk()` function creates the main window of the application, commonly referred to as the root window.
41+
42+
## 3. Creating a Label
43+
```python
44+
label = tk.Label(root, text="Hello, World!")
45+
label.pack()
46+
```
47+
The `Label()` function creates a text label widget. It can display text or images. The `pack()` method is used to place the widget within the window.
48+
49+
## 4. Creating a Button
50+
```python
51+
button = tk.Button(root, text="Click Me!", command=callback_function)
52+
button.pack()
53+
```
54+
The `Button()` function creates a clickable button widget. The `command` parameter specifies the function to be called when the button is clicked.
55+
56+
## 5. Creating an Entry (Input)
57+
```python
58+
entry = tk.Entry(root)
59+
entry.pack()
60+
```
61+
The `Entry()` function creates a single-line text entry widget where users can input text. It allows users to enter text interactively.
62+
63+
## 6. Creating a Frame
64+
```python
65+
frame = tk.Frame(root)
66+
frame.pack()
67+
```
68+
The `Frame()` function creates a container widget used to organize other widgets. It's useful for grouping related widgets together.
69+
70+
## 7. Layout Management
71+
Tkinter provides two main layout managers: grid and pack.
72+
### a. Grid Layout
73+
```python
74+
widget.grid(row=row_number, column=column_number)
75+
```
76+
The `grid()` method arranges widgets in rows and columns. Widgets are placed in specific rows and columns within the grid.
77+
### b. Pack Layout
78+
```python
79+
widget.pack(side="top" / "bottom" / "left" / "right")
80+
```
81+
The `pack()` method organizes widgets in a block fashion. Widgets are packed against one edge of the container widget.
82+
83+
## 8. Handling Events
84+
```python
85+
def callback_function():
86+
# Do something when the event occurs
87+
pass
88+
89+
button = tk.Button(root, text="Click Me!", command=callback_function)
90+
```
91+
Event handling in Tkinter involves defining functions to execute when certain events occur, such as button clicks or key presses. These functions are then associated with specific widgets.
92+
93+
## 9. Displaying Images
94+
```python
95+
photo = tk.PhotoImage(file="image.png")
96+
label = tk.Label(root, image=photo)
97+
label.pack()
98+
```
99+
Tkinter supports displaying images using the `PhotoImage()` class. Images can be displayed within labels or other widgets.
100+
101+
## 10. Dialog Boxes
102+
Tkinter provides various dialog boxes for user interaction.
103+
### a. Message Box
104+
```python
105+
import tkinter.messagebox as mbox
106+
mbox.showinfo("Title", "Message")
107+
```
108+
The `showinfo()` function displays an informational message box with a title and message.
109+
### b. Input Dialog Box
110+
```python
111+
result = mbox.askquestion("Title", "Question?")
112+
if result == "yes":
113+
# Do something if the user answers "yes"
114+
```
115+
The `askquestion()` function displays a dialog box with a question and returns the user's response.
116+
117+
## 11. Menu Bar
118+
```python
119+
menubar = tk.Menu(root)
120+
root.config(menu=menubar)
121+
122+
file_menu = tk.Menu(menubar, tearoff=0)
123+
file_menu.add_command(label="Open", command=open_file)
124+
file_menu.add_command(label="Save", command=save_file)
125+
126+
menubar.add_cascade(label="File", menu=file_menu)
127+
```
128+
Tkinter supports creating menu bars with cascading menus. Menu items can be added with commands associated with them.
129+
130+
## 12. Running the Application
131+
```python
132+
root.mainloop()
133+
```
134+
The `mainloop()` function starts the Tkinter event loop, allowing the application to handle user inputs and events.
135+
136+
## Example of a Simple Tkinter Program
137+
```python
138+
import tkinter as tk
139+
140+
def greeting():
141+
label.config(text="Hello, " + entry.get())
142+
143+
root = tk.Tk()
144+
145+
label = tk.Label(root, text="Enter your name:")
146+
label.pack()
147+
148+
entry = tk.Entry(root)
149+
entry.pack()
150+
151+
button = tk.Button(root, text="Greet", command=greeting)
152+
button.pack()
153+
154+
root.mainloop()
155+
```
156+
This is a basic example of a Tkinter program. It creates a window with a label, an entry field, and a button. When the button is clicked, it updates the label with a greeting message.
157+
158+
-----------------------------------------------------------------------------------------
159+
![Github Footer](https://github.com/shabir-mp/Kereta-Api-Indonesia-Booking-System/assets/133546000/c1833fe4-f470-494f-99e7-d583421625be)

0 commit comments

Comments
 (0)