鱼C论坛

 找回密码
 立即注册
查看: 628|回复: 2

[已解决]Tkinter background

[复制链接]
发表于 2024-9-22 10:21:07 | 显示全部楼层 |阅读模式

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

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

x
  1. from tkinter import *
  2. root=Tk()
  3. root.title("Tkinter : background")
  4. def gyl(a,b,c):
  5.           def abc():
  6.                     if a== "mediumseagreen":
  7.                               m.config(background=a)
  8.                     elif a=="mediumvioletred" :
  9.                               n.config(background=b)
  10.                     else:
  11.                               o.config(background=c)
  12.           m=Button(root,text="兰州",command=abc).pack()
  13.           n=Button(root,text="广州",command=abc).pack()
  14.           o=Button(root,text="杭州",command=abc).pack()
  15. gyl("mediumturquoise","mediumvioletred","mediumspringgreen")
  16. mainloop()
复制代码


最佳答案
2024-9-22 12:28:29
一定记得单独写组件定义
  1. from tkinter import *

  2. root = Tk()
  3. root.title("Tkinter: background")

  4. def gyl(a, b, c):
  5.     def abc():
  6.         if a == "mediumturquoise":
  7.             m.config(bg=a)
  8.         elif a == "mediumvioletred":
  9.             n.config(bg=b)
  10.         else:
  11.             o.config(bg=c)
  12.    
  13.     m = Button(root, text="兰州", command=abc)
  14.     m.pack()
  15.    
  16.     n = Button(root, text="广州", command=abc)
  17.     n.pack()
  18.    
  19.     o = Button(root, text="杭州", command=abc)
  20.     o.pack()

  21. gyl("mediumturquoise", "mediumvioletred", "mediumspringgreen")
  22. root.mainloop()
复制代码
无标题.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-9-22 10:21:20 | 显示全部楼层
你好!看起来你想创建一个带有按钮的Tkinter窗口,并根据按钮的点击改变背景颜色。我注意到代码中有一些问题,以下是修改后的代码:

  1. from tkinter import *

  2. root = Tk()
  3. root.title("Tkinter: background")

  4. def gyl(a, b, c):
  5.     def abc():
  6.         if a == "mediumturquoise":
  7.             m.config(bg=a)
  8.         elif a == "mediumvioletred":
  9.             n.config(bg=b)
  10.         else:
  11.             o.config(bg=c)
  12.    
  13.     m = Button(root, text="兰州", command=abc)
  14.     m.pack()
  15.    
  16.     n = Button(root, text="广州", command=abc)
  17.     n.pack()
  18.    
  19.     o = Button(root, text="杭州", command=abc)
  20.     o.pack()

  21. gyl("mediumturquoise", "mediumvioletred", "mediumspringgreen")
  22. root.mainloop()
复制代码


优化内容简要说明:
1. 根窗口定义移动至函数之外,以便更清晰地结构化代码。
2. 使用 `config(bg=...)` 修改背景颜色。
3. 在创建按钮时拆分了 `.pack()` 调用,确保每个按钮为独立对象,以便在 `abc` 函数中正确引用。

希望这对你有帮助!有什么问题可以继续提问哦!

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

使用道具 举报

发表于 2024-9-22 12:28:29 | 显示全部楼层    本楼为最佳答案   
一定记得单独写组件定义
  1. from tkinter import *

  2. root = Tk()
  3. root.title("Tkinter: background")

  4. def gyl(a, b, c):
  5.     def abc():
  6.         if a == "mediumturquoise":
  7.             m.config(bg=a)
  8.         elif a == "mediumvioletred":
  9.             n.config(bg=b)
  10.         else:
  11.             o.config(bg=c)
  12.    
  13.     m = Button(root, text="兰州", command=abc)
  14.     m.pack()
  15.    
  16.     n = Button(root, text="广州", command=abc)
  17.     n.pack()
  18.    
  19.     o = Button(root, text="杭州", command=abc)
  20.     o.pack()

  21. gyl("mediumturquoise", "mediumvioletred", "mediumspringgreen")
  22. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-21 22:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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