鱼C论坛

 找回密码
 立即注册
查看: 1671|回复: 4

[已解决]tkinter 中的字典

[复制链接]
发表于 2024-3-2 08:23:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. from tkinter import *
  2. root=Tk()
  3. root.geometry("563x543")
  4. a=[
  5.       {"name":"覆盖","relief":"sunken"},
  6.       {"name":"天空","relief":"raised"}]
  7. for i in range (len(a)) :
  8.       b=Button(root,
  9.                text=a["name"],
  10.                relief=a["relief"],
  11.                borderwidth=9,
  12.                width=11,
  13.                height=3,
  14.                justify="left",
  15.                wraplength=222,
  16.                font="汉仪哈哈体简  20  italic")
  17.       b.pack(expand=True)
  18. mainloop()
复制代码


问 题 出 在 第 9 句 和 第 1 0  句 , 应 该 如 何 修 改 ?
最佳答案
2024-3-2 09:25:42
本帖最后由 liuhongrun2022 于 2024-3-2 09:26 编辑

关于字体的那一行,我没有这个字体,请自行添加
  1. from tkinter import *
  2. root = Tk()
  3. root.geometry("563x543")

  4. a = [
  5.     {"name": "覆盖", "relief": "sunken"},
  6.     {"name": "天空", "relief": "raised"}
  7. ]

  8. for i in range(len(a)):
  9.     b = Button(root,
  10.                text=a[i]["name"], # 列表里应该先有索引,然后是字典里的键值对
  11.                relief=a[i]["relief"],
  12.                borderwidth=9,
  13.                width=11,
  14.                height=3,
  15.                justify="left",
  16.                wraplength=222,
  17.                )
  18.     b.pack(expand=True)

  19. mainloop()
复制代码

或者
  1. from tkinter import *
  2. root = Tk()
  3. root.geometry("563x543")

  4. a = [
  5.     {"name": "覆盖", "relief": "sunken"},
  6.     {"name": "天空", "relief": "raised"}
  7. ]

  8. for i in a: # 直接遍历列表
  9.     b = Button(root,
  10.                text=i["name"],
  11.                relief=i["relief"],
  12.                borderwidth=9,
  13.                width=11,
  14.                height=3,
  15.                justify="left",
  16.                wraplength=222,
  17.                )
  18.     b.pack(expand=True)

  19. mainloop()
复制代码

求个最佳答案

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-3-2 08:23:34 | 显示全部楼层
问题出在第9句和第10句中的变量`a`没有被定义。您可以定义一个字典,然后在循环中遍历字典的键值对来创建多个按钮。

以下是修正后的代码:

  1. from tkinter import *

  2. root = Tk()
  3. root.geometry("563x543")

  4. # 定义一个字典,包含按钮的属性和文本内容
  5. button_dict = {
  6.     "button1": {
  7.         "text": "Button 1",
  8.         "relief": "raised",
  9.         "borderwidth": 9,
  10.         "width": 11,
  11.         "height": 3,
  12.         "justify": "left",
  13.         "wraplength": 222,
  14.         "font": "汉仪哈哈体简 20 italic"
  15.     },
  16.     "button2": {
  17.         "text": "Button 2",
  18.         "relief": "sunken",
  19.         "borderwidth": 9,
  20.         "width": 11,
  21.         "height": 3,
  22.         "justify": "left",
  23.         "wraplength": 222,
  24.         "font": "汉仪哈哈体简 20 italic"
  25.     },
  26.     # 添加更多按钮的属性和文本内容...
  27. }

  28. for button_name, button_attr in button_dict.items():
  29.     b = Button(root, **button_attr)
  30.     b.pack(expand=True)

  31. mainloop()
复制代码


在`button_dict`字典中,每个键代表一个按钮的名称,每个值是一个字典,包含该按钮的属性和文本内容。然后我们使用`for`循环遍历`button_dict.items()`,以获取按钮的名称和属性字典,然后用`**`语法将属性字典作为参数传递给`Button()`函数来创建按钮。

请注意,我只提供了两个按钮的示例。您可以根据需要向`button_dict`中添加更多按钮的属性和文本内容。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-2 09:00:17 | 显示全部楼层
程 序 中 要 是 去 掉 第 6 句 ,就 可 以 的 。

目前,我需要2个按钮,“覆盖”和“天空” ,怎么弄啊 ?

要求:变量使用字典的形式,其它不行的!

  1. from tkinter import *
  2. root=Tk()
  3. root.geometry("563x543")
  4. a={"name":"覆盖","relief":"sunken"}
  5. for i in range (len(a)) :
  6.       b=Button(root,
  7.                text=a["name"],
  8.                relief=a["relief"],
  9.                borderwidth=9,
  10.                width=11,
  11.                height=3,
  12.                justify="left",
  13.                wraplength=222,
  14.                font="汉仪哈哈体简  20  italic")
  15.       b.pack(expand=True)
  16. mainloop()
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-2 09:00:33 | 显示全部楼层
dragov 发表于 2024-03-02 09:00
程 序 中 要 是 去 掉 第 6 句 ,就 可 以 的 。

...


如果您要使用字典来表示按钮的属性和文本内容,并且只需要两个按钮,"覆盖"和"天空",可以这样修改代码:from tkinter import *root = Tk()root.geometry("563x543")buttons = {    "button1": {        "text": "覆盖",        "relief": "sunken",        "borderwidth": 9,        "width": 11,        "height": 3,        "justify": "left",        "wraplength": 222,        "font": "汉仪哈哈体简 20 italic"    },    "button2": {        "text": "天空",        "relief": "raised",        "borderwidth": 9,        "width": 11,        "height": 3,        "justify": "left",        "wraplength": 222,        "font": "汉仪哈哈体简 20 italic"    }}for button_name, button_attr in buttons.items():    b = Button(root, **button_attr)    b.pack(expand=True)mainloop()在`buttons`字典中,每个键代表一个按钮的名称,每个值是一个字典,包含该按钮的属性和文本内容。然后我们使用`for`循环遍历`buttons.items()`,以获取按钮的名称和属性字典,然后用`**`语法将属性字典作为参数传递给`Button()`函数来创建按钮。代码中给出了两个按钮的示例,分别是"覆盖"和"天空"。您可以根据需要向`buttons`字典中添加更多按钮的属性和文本内容。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-2 09:25:42 | 显示全部楼层    本楼为最佳答案   
本帖最后由 liuhongrun2022 于 2024-3-2 09:26 编辑

关于字体的那一行,我没有这个字体,请自行添加
  1. from tkinter import *
  2. root = Tk()
  3. root.geometry("563x543")

  4. a = [
  5.     {"name": "覆盖", "relief": "sunken"},
  6.     {"name": "天空", "relief": "raised"}
  7. ]

  8. for i in range(len(a)):
  9.     b = Button(root,
  10.                text=a[i]["name"], # 列表里应该先有索引,然后是字典里的键值对
  11.                relief=a[i]["relief"],
  12.                borderwidth=9,
  13.                width=11,
  14.                height=3,
  15.                justify="left",
  16.                wraplength=222,
  17.                )
  18.     b.pack(expand=True)

  19. mainloop()
复制代码

或者
  1. from tkinter import *
  2. root = Tk()
  3. root.geometry("563x543")

  4. a = [
  5.     {"name": "覆盖", "relief": "sunken"},
  6.     {"name": "天空", "relief": "raised"}
  7. ]

  8. for i in a: # 直接遍历列表
  9.     b = Button(root,
  10.                text=i["name"],
  11.                relief=i["relief"],
  12.                borderwidth=9,
  13.                width=11,
  14.                height=3,
  15.                justify="left",
  16.                wraplength=222,
  17.                )
  18.     b.pack(expand=True)

  19. mainloop()
复制代码

求个最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-23 13:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表