dragov 发表于 2024-3-2 08:23:17

tkinter 中的字典


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="汉仪哈哈体简20italic")
      b.pack(expand=True)
mainloop()

问 题 出 在 第 9 句 和 第 1 0句 , 应 该 如 何 修 改 ?

FishC 发表于 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 小助理,如未能正确解答您的问题,请继续追问。

dragov 发表于 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="汉仪哈哈体简20italic")
      b.pack(expand=True)
mainloop()

FishC 发表于 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 小助理,如未能正确解答您的问题,请继续追问。

liuhongrun2022 发表于 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["name"], # 列表里应该先有索引,然后是字典里的键值对
               relief=a["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()

求个最佳答案
页: [1]
查看完整版本: tkinter 中的字典