ich baue grad für mein Spiel eine grafische Oberfläche. Nun habe ich bis jetzt soweit die grafische Oberfläche fast fertig.
Mein Ziel ist es, wenn der User auf einen Button klickt, dass dieser eine Hintergrundfarbe erhält und wenn der PC eine Nummer wählt, dass dann der passende Button des Computers auch eine Hintergrundfarbe bekommt.
Ich habe auch schon viel gegoogelt. Stehe nun aber vor einem Problem. Wie will ich der Funktion einen Parameter übergeben, wenn ich sie aber erst nach dem click auf dem Button ausführen möchte ?
Mein Script:
Code: Alles auswählen
import tkinter as tk
def changeColor(button):
button.configure(background='beige')
def startGame():
root.destroy()
game = tk.Tk()
sizeX = 500
sizeY = 350
game.geometry("{}x{}".format(sizeX, sizeY))
game.title("Shear Stone Paper")
roundNumber = 1
playerName = "Hans"
rounds = tk.Label(game, text="Round {}".format(roundNumber))
player = tk.Label(game, text="{}".format(playerName))
computer = tk.Label(game, text="Computer")
# Shear Stone Paper (Auswahlmöglichkeiten)
shearButton = tk.Button(game, text="Shear")
stoneButton = tk.Button(game, text="Stone")
paperButton = tk.Button(game, text="Paper")
shearButton.bind('<Button-1>', changeColor) # <--- hier
shearButton2 = tk.Button(game, text="Shear", state=tk.DISABLED)
stoneButton2 = tk.Button(game, text="Stone", state=tk.DISABLED)
paperButton2 = tk.Button(game, text="Paper", state=tk.DISABLED)
rounds.place(x = 250, y = 0)
player.place(x = 65, y = 20)
computer.place(x = 390, y = 20)
shearButton.place(x = 60, y = 60)
shearButton2.place(x = 400, y = 60)
stoneButton.place(x = 60, y = 100)
stoneButton2.place(x = 400, y = 100)
paperButton.place(x = 60, y = 140)
paperButton2.place(x = 400, y = 140)
game.mainloop()
sizeX = 500
sizeY = 150
# erzeugt das Root-Widget, dies liefert die Titelleiste und die Dekoration
root = tk.Tk()
root.geometry("{}x{}".format(sizeX, sizeY))
# title setzen
root.title("Shear Stone Paper")
# erzeugt ein Label
header = tk.Label(root, text="Game: Shear Stone Paper")
header.pack()
frame = tk.Frame(root)
frame.pack()
buttonStart = tk.Button(frame, text="Start Game", command=startGame)
buttonStart.pack(side=tk.LEFT, padx=5, pady=5)
buttonQuit = tk.Button(frame, text="Quit Game", command=root.quit)
buttonQuit.pack(side=tk.LEFT, padx=5, pady=5)
descriptionHeader = tk.Label(root, text="Gamedescription:")
description = tk.Label(root, text="This is the classic Game of Shear Stone Paper.\n You are playing against the Computer.\n The Target is to get 3 Points and win this Game.")
descriptionHeader.pack()
description.pack(side=tk.TOP)
# Fenster bleibt solange offen bis das Fenster geschlossen wird.
root.mainloop()
Moon