python tkinter set
时间: 2023-10-19 09:11:50 浏览: 95
To set a value in a tkinter widget, you need to use the `set()` method. The `set()` method is available for many different tkinter widgets, including `StringVar()`, `IntVar()`, `DoubleVar()`, `BooleanVar()`, `Listbox`, and `Text`.
Here is an example of how to use the `set()` method to set the value of a `StringVar()`:
```
import tkinter as tk
root = tk.Tk()
my_string_var = tk.StringVar()
my_string_var.set("Hello, world!")
my_label = tk.Label(root, textvariable=my_string_var)
my_label.pack()
root.mainloop()
```
In this example, we create a `StringVar()` called `my_string_var` and use the `set()` method to set its value to "Hello, world!". We then create a `Label` widget and pass in `my_string_var` as the `textvariable` argument. When the label is displayed on the screen, it will show the value of `my_string_var`.
You can use the `set()` method in a similar way for other tkinter widgets that have a `set()` method available.
阅读全文