python 3 tkinter
时间: 2023-10-19 17:29:40 浏览: 87
Tkinter is a Python library that provides a set of tools for building graphical user interfaces (GUIs). It is included with most Python installations, so you don't need to install any additional packages to use it. Tkinter is a cross-platform library, meaning you can create GUIs that work on different operating systems.
To start using Tkinter, you need to import the library by adding the following line at the beginning of your Python script:
```python
import tkinter as tk
```
Once you have imported Tkinter, you can create the main window of your application by creating an instance of the `Tk` class:
```python
window = tk.Tk()
```
From there, you can add various widgets such as buttons, labels, text boxes, etc., to the window using the available Tkinter classes. You can then configure their properties and handle events.
Here's a simple example that creates a window with a label and a button:
```python
import tkinter as tk
def button_clicked():
label.config(text="Button clicked!")
window = tk.Tk()
label = tk.Label(window, text="Hello, Tkinter!")
label.pack()
button = tk.Button(window, text="Click me", command=button_clicked)
button.pack()
window.mainloop()
```
In this example, when the button is clicked, the `button_clicked` function is called, which updates the text of the label.
This is just a basic introduction to Tkinter. There are many more features and widgets available to create more complex GUIs. I hope this helps you get started with Python 3 and Tkinter!
阅读全文