【问题标题】:How can I split one file into 2 small ones tkinter gui?如何将一个文件拆分为 2 个小文件 tkinter gui?
【发布时间】:2020-07-24 17:42:01
【问题描述】:

我有一个名为 ran.py 的文件,其中创建了一个按钮,该按钮应更改主窗口的颜色:

from tkinter import *
import gui
win = Tk()

b = Button(win, command=color)
b.pack()

mainloop()

在一个名为 gui.py 的文件中,我有一个应该更改背景颜色的命令:

win.configure(bg="red")

我尝试了很多方法,但总是失败并出现以下错误: win.configure(bg="red") NameError: name 'win' is not defined 如何让点击时背景颜色发生变化?

【问题讨论】:

    标签: python-3.x tkinter


    【解决方案1】:

    我猜你在gui.py 中有一个color() 函数,如下所示:

    def color():
        win.configure(bg='red')
    

    bcommand 选项应如下所示:

    b = Button(win, command=gui.color)
    

    如果是这种情况,您将收到上述错误,因为win 不在gui 范围内。

    您应该将win 作为参数传递给color() 内的gui.py

    def color(win):
        win.configure(bg='red')
    

    然后修改command选项如下:

    b = Button(win, command=lambda: gui.color(win))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多