鱼C论坛

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

[已解决]tkinter 中的字典

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

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

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

x
from tkinter import *
root=Tk()
root.geometry("563x543")
a=[
      {"name":"覆盖","relief":"sunken"},
      {"name":"天空","relief":"raised"}]
for i in range (len(a)) :
      b=Button(root,
               text=a["name"],
               relief=a["relief"],
               borderwidth=9,
               width=11,
               height=3,
               justify="left",
               wraplength=222,
               font="汉仪哈哈体简  20  italic")
      b.pack(expand=True)
mainloop()

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

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

a = [
    {"name": "覆盖", "relief": "sunken"},
    {"name": "天空", "relief": "raised"}
]

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

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

a = [
    {"name": "覆盖", "relief": "sunken"},
    {"name": "天空", "relief": "raised"}
]

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

mainloop()
求个最佳答案

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

以下是修正后的代码:
from tkinter import *

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

# 定义一个字典,包含按钮的属性和文本内容
button_dict = {
    "button1": {
        "text": "Button 1",
        "relief": "raised",
        "borderwidth": 9,
        "width": 11,
        "height": 3,
        "justify": "left",
        "wraplength": 222,
        "font": "汉仪哈哈体简 20 italic"
    },
    "button2": {
        "text": "Button 2",
        "relief": "sunken",
        "borderwidth": 9,
        "width": 11,
        "height": 3,
        "justify": "left",
        "wraplength": 222,
        "font": "汉仪哈哈体简 20 italic"
    },
    # 添加更多按钮的属性和文本内容...
}

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

mainloop()

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

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

要求:变量使用字典的形式,其它不行的!
from tkinter import *
root=Tk()
root.geometry("563x543")
a={"name":"覆盖","relief":"sunken"}
for i in range (len(a)) :
      b=Button(root,
               text=a["name"],
               relief=a["relief"],
               borderwidth=9,
               width=11,
               height=3,
               justify="left",
               wraplength=222,
               font="汉仪哈哈体简  20  italic")
      b.pack(expand=True)
mainloop()

想知道小甲鱼最近在做啥?请访问 -> 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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

a = [
    {"name": "覆盖", "relief": "sunken"},
    {"name": "天空", "relief": "raised"}
]

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

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

a = [
    {"name": "覆盖", "relief": "sunken"},
    {"name": "天空", "relief": "raised"}
]

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

mainloop()
求个最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-22 08:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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