我正在用 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 片段:
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"
我一直在调整它,但只是收到不同的错误消息。如果有人可以提供帮助,我将不胜感激。