在Frame中加入1个滚动列表框list、1个下拉列表框Choice和1个按钮,点击按钮将list中的项目移到Choice组件中
时间: 2024-05-04 08:18:05 浏览: 18
好的,可以使用如下的代码实现:
```python
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
# 创建滚动列表框list
self.listbox = tk.Listbox(self, height=5)
for item in ['apple', 'banana', 'cherry', 'date', 'elderberry']:
self.listbox.insert(tk.END, item)
self.listbox.pack(side=tk.LEFT, padx=10, pady=10)
# 创建下拉列表框Choice
self.choice = tk.StringVar(self)
self.choice.set('select')
self.dropdown = tk.OptionMenu(self, self.choice, 'select', 'option1', 'option2', 'option3')
self.dropdown.pack(side=tk.LEFT, padx=10, pady=10)
# 创建按钮
self.button = tk.Button(self, text='Move to Choice', command=self.move_to_choice)
self.button.pack(side=tk.LEFT, padx=10, pady=10)
def move_to_choice(self):
# 将list中选中的项目移动到Choice组件中
selected_items = self.listbox.curselection()
for index in selected_items[::-1]:
item = self.listbox.get(index)
self.choice.set(item)
self.listbox.delete(index)
root = tk.Tk()
app = Application(master=root)
app.mainloop()
```
这段代码创建了一个包含滚动列表框`Listbox`、下拉列表框`OptionMenu`和按钮`Button`的应用程序窗口,当用户点击按钮时,将选中的项目从列表框移动到下拉列表框中。其中`Listbox`和`OptionMenu`都是Tkinter中的标准组件,`Button`是一个按钮,`StringVar`是一个Tkinter变量,用于保存`OptionMenu`中选中的值。