利用tkinker进行GUI编程,图形界面显示以下淡水鱼数据
时间: 2023-09-10 17:07:28 浏览: 52
好的,以下是一个简单的利用tkinter实现的GUI界面,可以显示淡水鱼数据:
```python
import tkinter as tk
# 淡水鱼数据
fish_data = [
{"name": "锦鲤", "color": "红色", "price": 50},
{"name": "孔雀鱼", "color": "蓝色", "price": 30},
{"name": "神仙鱼", "color": "白色", "price": 100},
{"name": "金鱼", "color": "金色", "price": 10},
{"name": "鲫鱼", "color": "灰色", "price": 20}
]
# 创建主窗口
root = tk.Tk()
root.title("淡水鱼数据")
# 创建列表框
listbox = tk.Listbox(root, width=50, height=10)
listbox.pack(padx=10, pady=10)
# 添加淡水鱼数据到列表框
for fish in fish_data:
listbox.insert(tk.END, f"{fish['name']},{fish['color']},{fish['price']}元")
# 运行主循环
root.mainloop()
```
运行上述代码后,会弹出一个窗口,里面显示了淡水鱼的名称、颜色和价格信息。你可以根据需要修改数据或者界面布局。
相关问题
编程实现:利用tkinker进行GUI编程,图形界面显示以下淡水鱼数据。 已知淡水鱼数据 data = [ ['f1','鲤鱼','杂食性',300], ['f2','草鱼','食草性',240], ['f3','鲫鱼','温水性',500], ['f4','鳙鱼','温水性',400], ['f5','黑鱼','肉食性',480], ]
以下是利用tkinker进行GUI编程,图形界面显示淡水鱼数据的代码:
```python
import tkinter as tk
data = [
['f1', '鲤鱼', '杂食性', 300],
['f2', '草鱼', '食草性', 240],
['f3', '鲫鱼', '温水性', 500],
['f4', '鳙鱼', '温水性', 400],
['f5', '黑鱼', '肉食性', 480],
]
root = tk.Tk()
root.title('淡水鱼数据')
# 创建表格
for i in range(len(data)):
for j in range(len(data[i])):
cell = tk.Entry(root, width=12, fg='blue', font=('Arial', 14, 'bold'))
cell.grid(row=i, column=j)
cell.insert(0, data[i][j])
# 运行窗口
root.mainloop()
```
运行后,会弹出一个窗口,显示如下淡水鱼数据的表格:
| f1 | 鲤鱼 | 杂食性 | 300 |
| -- | ---- | ------ | --- |
| f2 | 草鱼 | 食草性 | 240 |
| f3 | 鲫鱼 | 温水性 | 500 |
| f4 | 鳙鱼 | 温水性 | 400 |
| f5 | 黑鱼 | 肉食性 | 480 |
1.编程实现:利用tkinker进行GUI编程,图形界面显示以下淡水鱼数据。 已知淡水鱼数据 data = [ ['f1','鲤鱼','杂食性',300], ['f2','草鱼','食草性',240], ['f3','鲫鱼','温水性',500], ['f4','鳙鱼','温水性',400], ['f5','黑鱼','肉食性',480], ]
以下是基于Tkinter的GUI编程实现代码:
```python
import tkinter as tk
data = [
['f1', '鲤鱼', '杂食性', 300],
['f2', '草鱼', '食草性', 240],
['f3', '鲫鱼', '温水性', 500],
['f4', '鳙鱼', '温水性', 400],
['f5', '黑鱼', '肉食性', 480],
]
root = tk.Tk()
root.title('淡水鱼数据')
# 创建表格标题
tk.Label(root, text='编号').grid(row=0, column=0)
tk.Label(root, text='名称').grid(row=0, column=1)
tk.Label(root, text='饮食').grid(row=0, column=2)
tk.Label(root, text='重量(g)').grid(row=0, column=3)
# 输出数据
for i in range(len(data)):
for j in range(len(data[i])):
# 设置单元格宽度
width = 15 if j == 1 else 10
tk.Label(root, text=data[i][j], width=width).grid(row=i+1, column=j)
root.mainloop()
```
代码解析:
1. 首先我们导入了tkinter模块,并定义了需要显示的数据data。
2. 接着,我们创建了一个Tkinter窗口,并设置标题为“淡水鱼数据”。
3. 然后,我们在窗口中创建了一个表格,并设置了表格的标题。
4. 最后,我们循环遍历数据,将每个单元格的数据输出到表格中。
5. 最后调用mainloop()方法,使窗口进入事件循环,等待用户操作。
以上就是利用Tkinter进行GUI编程的代码实现。