0
昨晚我開始了一個迷你項目,我想用tkinter
做一個遊戲(與pygame
相對,主要是因爲我不知道如何在pygame
中製作多個窗口)。RecursionError試圖改變python tkinter中多個窗口的背景
我想讓用戶猜測RGB值並計算它們的準確度。然而,每當我運行我的代碼時,我在startgame
函數中得到一個RecursionError
,並且沒有任何顏色改變。我很困惑,爲什麼。
from tkinter import *
import random
class multiWindow(Tk):
def __init__(self):
# ******* CREATE WINDOWS *******
window1 = Tk()
window2 = Tk()
window3 = Tk()
# ******* WINDOW DIMENSIONS *******
windowH = 100
windowW = 250
# ******* SCREEN RES *******
screenW = 1366-windowW
screenH = 768-windowH
# ******* COORDINATES OF WINDOWS *******
window1.geometry('%dx%d+%d+%d' % (windowW,windowH,screenW/6,screenH/3))
window2.geometry('%dx%d+%d+%d' % (windowW,windowH,screenW/2,screenH/3))
window3.geometry('%dx%d+%d+%d' % (windowW,windowH,screenW/(6/5),screenH/3))
# ******* WINDOW TITLES *******
window1.title("Color1")
window2.title("Color2")
window3.title("Color3")
# ******** BUTTON START *******
buttonStart = Button(window1,text="Start Game", command=lambda: self.startGame())
buttonStart.pack()
# ******* OPEN WINDOWS *******
window1.mainloop()
window2.mainloop()
window3.mainloop()
# ******** PRODUCE RANDOM COLOR ********
def randomRGB(self):
return random.randint(0,255)
def guessRed(self,window):
while True:
try:
print("I want you to guess the RGB values of the colors in the windows above.")
print("Guess the red value of the window displaying ",window)
guess = int(input())
if guess < 0 or guess > 255:
raise
else:
return guess
break
except:
print("Error")
def startGame(self):
# ******** DETERMINE COLOR VALUES *******
colorVal = [0,0,0,0,0,0,0,0,0]
for x in range(0,len(colorVal)):
colorVal[x] = self.randomRGB()
# ******* GIVE WINDOWS COLOR *******
self.window1.configure(background='#%02x%02x%02x'%(colorVal[0],colorVal[1],colorVal[2],))
self.window2.configure(background='#%02x%02x%02x'%(colorVal[3],colorVal[4],colorVal[5],))
self.window3.configure(background='#%02x%02x%02x'%(colorVal[6],colorVal[7],colorVal[8],))
multiWindow = multiWindow()
print("This comes after multiwindow class")
我在tkinter
沒有很好的逆轉。剛剛開始閱讀關於它的2天前,但我覺得這個問題與我對Python的掌握沒有什麼關係。
任何幫助是極大的讚賞
'multiWindow = multiWindow()'不要使用與類相同名稱的變量。這可能與你的具體問題沒有任何關係,但這肯定是個問題。 –