【问题标题】:Getting two Python scripts to work together让两个 Python 脚本协同工作
【发布时间】:2013-05-28 15:45:52
【问题描述】:

我正在使用 pygame 制作一个简单的绘图板程序,并且我正在制作它以便您可以定义自己的颜色。我正在使用 tk 窗口来执行此操作,但我无法弄清楚如何让它们一起工作。请帮忙,我一直在努力让它工作几个小时

这是我的 Tk 代码:

from Tkinter import *
r = 0
g = 0
b = 0

class Custom():
    def get_color(self):        
        self.root = Tk()
        self.root.configure(background='black')
        self.root.wm_title("Custom")

        label1 = Label(self.root, text='Red Value:',bg="black", fg="white")
        label1.grid(row=2, column=0,columnspan=2)
        self.enter1 = Entry(self.root, bg='white')
        self.enter1.grid(row=3, column=0,columnspan=2)


        label2 = Label(self.root, text='Green Value:',bg="black", fg="white")
        label2.grid(row=4, column=0,columnspan=2)
        self.enter2 = Entry(self.root, bg='white')
        self.enter2.grid(row=5, column=0, columnspan=2)

        label3 = Label(self.root, text='Blue Value:',bg="black", fg="white")
        label3.grid(row=6, column=0,columnspan=2)
        self.enter3 = Entry(self.root, bg='white')
        self.enter3.grid(row=7, column=0, columnspan=2)

        btn1 = Button(self.root, text='OK', command=self.close, bg="black",activebackground="green", fg="white")
        btn1.grid(row=14, column=0, columnspan=2)
        label7 = Label(self.root, bg="black", fg = "white")
        label7.grid(row=15, column=0, columnspan=2)

        self.enter1.focus()

        self.root.mainloop()

    def close(self):
        self.root.destroy()

    def return_color(self):
        r = int(self.enter1.get())
        g = int(self.enter2.get())
        b = int(self.enter3.get())
        return (r,g,b)

它工作正常,但我无法让它将三个值返回到我的 pygame 程序。

这里是 pygame sn-p:

if key[pygame.K_c]:
    import CustomColor
    c = CustomColor.Custom()
    c.get_color()
    self.color = c.return_color()

当前错误:

Traceback (most recent call last):
  File "C:/Python27/Drawpad.py", line 75, in <module>
    draw.main()
  File "C:/Python27/Drawpad.py", line 69, in main
    self.update(screen)
  File "C:/Python27/Drawpad.py", line 45, in update
    self.color = c.return_color()
  File "C:/Python27\CustomColor.py", line 41, in return_color
    r = int(self.enter1.get())
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2391, in get
    return self.tk.call(self._w, 'get')
 TclError: invalid command name ".19783112"

我一直在调整它,但只是收到不同的错误消息。如果有人可以提供帮助,我将不胜感激。

【问题讨论】:

  • 如果您发布错误消息,更有可能有人会帮助您。
  • 我有很多,因为我改变了我的代码这么多
  • 当你现在运行它时,它只会产生一个错误信息。发布那个。如果您有另一个版本会生成另一条错误消息,请也发布该消息。
  • 好吧,我必须改回我的代码,我已经从这里调整过了
  • 哦,所以我应该在我的 pygame 窗口中弹出一个框来接受输入吗?

标签: python class function colors tkinter


【解决方案1】:

您的直接问题是您试图在 Entry 对象被销毁后访问它们。

你可以很简单地解决这个问题:

def close(self):
    self.r = int(self.enter1.get())
    self.g = int(self.enter2.get())
    self.b = int(self.enter3.get())
    self.root.destroy()

def return_color(self):
    return (self.r,self.g,self.b)

发生这种情况的原因是,当您调用 get_color 时,它会调用 self.root.mainloop,这意味着它不可能返回,直到 Tk 事件循环退出,这意味着调用您的 close 方法,它表示self.root.destroy 保证在您调用return_color 之前已经发生。

调用阻塞 Tk 事件循环的函数也会有其他问题。您的 pygame 事件循环被卡在等待 Tk 中。因此,您的 pygame 窗口无法重绘自身、最小化或响应任何其他事件。

【讨论】:

  • 感谢您的回答,但我认为像您一样使用 pygame 可能会更容易,非常感谢!!
猜你喜欢
  • 2016-01-28
  • 1970-01-01
  • 2019-05-30
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多