私は現在、tkinterのエントリウィジェットを使用してユーザから数値を取得しようとしています。その番号を使用して、他の関数のパラメータを定義しています。Python tkinterエントリウィジェットの使用
コードは非常に長いので、ブロックの後の私の考えを要約しようとします。
class NimGame():
def __init__(self, numberOfBalls):
self.numberOfBallsRemaining = numberOfBalls
print("Nim game initialized with {} balls.".format(self.numberOfBallsRemaining))
def remainingBalls(self):
return self.numberOfBallsRemaining
def take(self, numberOfBalls):
if (numberOfBalls < 1) or (numberOfBalls > 3) or (numberOfBalls > self.numberOfBallsRemaining):
print("You can't take that number of balls. Try again.")
# ## Update Label in the GUI to tell user they can't take that many balls.
# It might be better to "inactivate" the buttons that correspond to invalid number to take.
statusLabel.configure(text="You can't take that number of balls. Try again.")
else:
self.numberOfBallsRemaining = self.numberOfBallsRemaining - numberOfBalls
print("You took {} balls. {} remain.".format(numberOfBalls, self.numberOfBallsRemaining))
if self.numberOfBallsRemaining == 0:
print("Computer wins!")
else:
# ## After removing player-chosen balls, update graphics and then pause for 1.0 seconds
# before removing computer-chosen balls. This way the player can "see" the
# intermediate status. Perhaps think about a nicer way of showing this.
updateGraphics()
sleep(1.0)
computerMaxBalls = min(3, self.numberOfBallsRemaining)
compBallsTaken = random.randint(1,computerMaxBalls)
self.numberOfBallsRemaining = self.numberOfBallsRemaining - compBallsTaken
# ## After removing computer-chosen balls, update graphics again.
updateGraphics()
print("Computer took {} balls. {} remain.".format(compBallsTaken, self.numberOfBallsRemaining))
if self.numberOfBallsRemaining == 0:
print("You win!")
def updateGraphics():
canvas.delete('all')
centerX = leftmostBallXPosition
centerY = ballYPosition
for i in range(nimGame.remainingBalls()):
canvas.create_oval(centerX - halfBallSize,
centerY - halfBallSize,
centerX + halfBallSize,
centerY + halfBallSize,
fill="#9999ff")
centerX = centerX + spaceBetweenBalls + ballSize
canvas.update_idletasks()
def initializeNewGame():
numberOfBalls = e1.get()
initializeNimAndGUI(numberOfBalls)
def initializeNimAndGUI(numberOfBalls):
global nimGame
global ballSize, halfBallSize, spaceBetweenBalls, leftmostBallXPosition, ballYPosition
nimGame = NimGame(numberOfBalls)
canvas.delete('all')
ballSize = min(maxBallSize, int(((canvasWidth-canvasBorderBuffer)//numberOfBalls)/1.2))
halfBallSize = ballSize // 2
spaceBetweenBalls = int(0.2 * ballSize)
leftmostBallXPosition = (canvasBorderBuffer//2) + (spaceBetweenBalls//2) + halfBallSize
ballYPosition = canvasHeight // 2
updateGraphics()
def createGUI():
global rootWindow
global canvas
global statusLabel
global textEntry
global e1
rootWindow = Tk()
canvasAndButtons = Frame(rootWindow)
canvas = Canvas(canvasAndButtons, height=canvasHeight, width=canvasWidth, relief=SUNKEN, borderwidth=2)
canvas.pack(side=LEFT)
buttonframe = Frame(canvasAndButtons)
e1 = Entry(buttonframe)
e1.pack()
button1 = Button(buttonframe, text='Take 1', command=lambda:takeBalls(1))
button2 = Button(buttonframe, text='Take 2', command=lambda:takeBalls(2))
button3 = Button(buttonframe, text='Take 3', command=lambda:takeBalls(3))
button4 = Button(buttonframe, text='New Game', command=initializeNewGame)
button1.pack()
button2.pack()
button3.pack()
button4.pack()
buttonframe.pack(side=RIGHT)
canvasAndButtons.pack()
statusLabel = Label(rootWindow, text="Play Nim")
statusLabel.pack()
def runNim(numberOfBalls):
createGUI()
initializeNimAndGUI(numberOfBalls)
rootWindow.mainloop()
私には、2番目のゲームをするときにグラフィックが更新される問題があるようです。起こるべきことは、例えば、私はrunNim(20)を呼び出し、20個のボールでゲームをプレイするということです。ゲーム終了後、エントリーウィジェットに数字を入力して新しいゲームをクリックし、それを新しいnumberOfBallsにする必要があります。私がそれをすると、というメッセージが返ってきます。「10個のボールで初期化されたゲーム」しかし、GUIは変わらず、GUIにボールが表示されず、何かを取ろうとしてもトレースバックやエラーが発生します。
e1.get()を試しましたか?これにより、入力ボックスにユーザーが入力した値が読み込まれます。また、トレースバックエラーは何ですか? \ __ call__ リターンself.func(*引数)で – AR06
ファイル "情報ファイル"、ライン1549、 initializeNewGame initializeNimAndGUI(numberofBalls) ファイルの "ファイル情報" のファイル "情報ファイル"、ライン86、ライン102 、initalizeNimAndGUI ballSize = min(maxBallSize、int((canvasWidth-canvasBorderBuffer)// numberOfBalls)/1.2)) – CabooseMSG
私のnumberOfBallsでは、私はe1.get()を持っています。しかし、私は文字列を返すと言われており、intが必要です。その状況でintを返すにはどうすればよいですか? – CabooseMSG