Ich komme heute mit einem Problem aus dem Informatikunterricht zu euch. Unser erstes großes Projekt mit python und das allererste mit tkinter schlechthin, war es eine Version des beliebten Brettspiels Reversi zu machen. Deswegen entschuldigt die spartanische Programmierweise, wir sind halt blutige Anfänger.
Wir fragen uns, wie wir es auf die Weise, wie wir das ganze angefangen haben, am Besten hinbekommen, dass wir die Buttons wahlweise weiss oder schwarz einfärben können. Unsere Idee war es, das ganze über eine Unterscheidung von Links- und Rechtsklick zu lösen (Linksklick: schwarz, Rechtsklick: weiss). Doch wie funktioniert das? Und wenn das unmöglich ist, wie funktioniert das anders?
Code: Alles auswählen
from tkinter import *
#Def's
def buttonWeiterClick():
stand = int(labelZahl.cget("text"))
stand = stand + 1
labelZahl.config(text=str(stand))
def a1_klick():
a1.configure(bg="#000000")
p=1
text_Player.configure(text=Player)
def a2_klick():
a2.configure(bg="#000000")
def a3_klick():
a3.configure(bg="#000000")
def a4_klick():
a4.configure(bg="#000000")
def a5_klick():
a5.configure(bg="#000000")
def a6_klick():
a6.configure(bg="#000000")
def a7_klick():
a7.configure(bg="#000000")
def a8_klick():
a8.configure(bg="#000000")
def b1_klick():
b1.configure(bg="#000000")
def b2_klick():
b2.configure(bg="#000000")
def b3_klick():
b3.configure(bg="#000000")
def b4_klick():
b4.configure(bg="#000000")
def b5_klick():
b5.configure(bg="#000000")
def b6_klick():
b6.configure(bg="#000000")
def b7_klick():
b7.configure(bg="#000000")
def b8_klick():
b8.configure(bg="#000000")
def c1_klick():
c1.configure(bg="#000000")
def c2_klick():
c2.configure(bg="#000000")
def c3_klick():
c3.configure(bg="#000000")
def c4_klick():
c4.configure(bg="#000000")
def c5_klick():
c5.configure(bg="#000000")
def c6_klick():
c6.configure(bg="#000000")
def c7_klick():
c7.configure(bg="#000000")
def c8_klick():
c8.configure(bg="#000000")
def d1_klick():
d1.configure(bg="#000000")
def d2_klick():
d2.configure(bg="#000000")
def d3_klick():
d3.configure(bg="#000000")
def d4_klick():
d4.configure(bg="#000000")
def d5_klick():
d5.configure(bg="#000000")
def d6_klick():
d6.configure(bg="#000000")
def d7_klick():
d7.configure(bg="#000000")
def d8_klick():
d8.configure(bg="#000000")
def e1_klick():
e1.configure(bg="#000000")
def e2_klick():
e2.configure(bg="#000000")
def e3_klick():
e3.configure(bg="#000000")
def e4_klick():
e4.configure(bg="#000000")
def e5_klick():
e5.configure(bg="#000000")
def e6_klick():
e6.configure(bg="#000000")
def e7_klick():
e7.configure(bg="#000000")
def e8_klick():
e8.configure(bg="#000000")
def f1_klick():
f1.configure(bg="#000000")
def f2_klick():
f2.configure(bg="#000000")
def f3_klick():
f3.configure(bg="#000000")
def f4_klick():
f4.configure(bg="#000000")
def f5_klick():
f5.configure(bg="#000000")
def f6_klick():
f6.configure(bg="#000000")
def f7_klick():
f7.configure(bg="#000000")
def f8_klick():
f8.configure(bg="#000000")
def g1_klick():
g1.configure(bg="#000000")
def g2_klick():
g2.configure(bg="#000000")
def g3_klick():
g3.configure(bg="#000000")
def g4_klick():
g4.configure(bg="#000000")
def g5_klick():
g5.configure(bg="#000000")
def g6_klick():
g6.configure(bg="#000000")
def g7_klick():
g7.configure(bg="#000000")
def g8_klick():
g8.configure(bg="#000000")
def h1_klick():
h1.configure(bg="#000000")
def h2_klick():
h2.configure(bg="#000000")
def h3_klick():
h3.configure(bg="#000000")
def h4_klick():
h4.configure(bg="#000000")
def h5_klick():
h5.configure(bg="#000000")
def h6_klick():
h6.configure(bg="#000000")
def h7_klick():
h7.configure(bg="#000000")
def h8_klick():
h8.configure(bg="#000000")
# GUI-Objekte
p=0
if p==0:
Player="Spieler 1 (Schwarz)"
else:
Player="Spieler 2 (Weiß)"
# Fenster
Haupt = Tk()
Haupt.title("Othello v0.0.4")
Haupt.geometry("810x810")
Haupt.resizable(width=False,height=False)
C_1=PhotoImage(file="s.gif")
C_2=PhotoImage(file="w.gif")
l_1=PhotoImage(file="l_1.gif")
l_2=PhotoImage(file="l_2.gif")
# Grundstruktur
# Raster:
canvas=Canvas(master=Haupt, bg="#245f11")
canvas.place(x=0, y=0, width=810, height=810)
# Vertikalen
canvas.create_line(105,105,105,705)
canvas.create_line(180,105,180,705)
canvas.create_line(255,105,255,705)
canvas.create_line(330,105,330,705)
canvas.create_line(405,105,405,705)
canvas.create_line(480,105,480,705)
canvas.create_line(555,105,555,705)
canvas.create_line(630,105,630,705)
canvas.create_line(705,105,705,705)
# Horizontalen
canvas.create_line(105,105,705,105)
canvas.create_line(105,180,705,180)
canvas.create_line(105,255,705,255)
canvas.create_line(105,330,705,330)
canvas.create_line(105,405,705,405)
canvas.create_line(105,480,705,480)
canvas.create_line(105,555,705,555)
canvas.create_line(105,630,705,630)
canvas.create_line(105,705,705,705)
# "Steinelager":
L_1=Label(master=Haupt, image=l_1,bg="#245f11")
L_1.place(x=5,y=5,width=80,height=80)
L_2=Label(master=Haupt, image=l_2, bg="#245f11")
L_2.place(x=723,y=723,width=80,height=80)
# Textfelder:
# Steinelager
text_S_1=Label(master=Haupt,text="30",font=("Arial",32),bg="#245f11",fg="#000000")
text_S_1.place(x=90,y=5,width=45,height=80)
text_S_2=Label(master=Haupt,text="30",font=("Arial",32),bg="#245f11",fg="#ffffff")
text_S_2.place(x=673,y=723,width=45,height=80)
# Spieler
text_Player=Button(master=Haupt,text=Player,font=("Arial",32),bg="#245f11",fg="#e6cf00")
text_Player.place(x=256,y=20)
# Buttons:
# A-Zeile
a1=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=a1_klick)
a1.place(x=106,y=106,width=74,height=74)
a2=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=a2_klick)
a2.place(x=181,y=106,width=74,height=74)
a3=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=a3_klick)
a3.place(x=256,y=106,width=74,height=74)
a4=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=a4_klick)
a4.place(x=331,y=106,width=74,height=74)
a5=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=a5_klick)
a5.place(x=406,y=106,width=74,height=74)
a6=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=a6_klick)
a6.place(x=481,y=106,width=74,height=74)
a7=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=a7_klick)
a7.place(x=556,y=106,width=74,height=74)
a8=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=a8_klick)
a8.place(x=631,y=106,width=74,height=74)
# B-Zeile
b1=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=b1_klick)
b1.place(x=106,y=181,width=74,height=74)
b2=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=b2_klick)
b2.place(x=181,y=181,width=74,height=74)
b3=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=b3_klick)
b3.place(x=256,y=181,width=74,height=74)
b4=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=b4_klick)
b4.place(x=331,y=181,width=74,height=74)
b5=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=b5_klick)
b5.place(x=406,y=181,width=74,height=74)
b6=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=b6_klick)
b6.place(x=481,y=181,width=74,height=74)
b7=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=b7_klick)
b7.place(x=556,y=181,width=74,height=74)
b8=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=b8_klick)
b8.place(x=631,y=181,width=74,height=74)
# C-Zeile
c1=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=c1_klick)
c1.place(x=106,y=256,width=74,height=74)
c2=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=c2_klick)
c2.place(x=181,y=256,width=74,height=74)
c3=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=c3_klick)
c3.place(x=256,y=256,width=74,height=74)
c4=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=c4_klick)
c4.place(x=331,y=256,width=74,height=74)
c5=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=c5_klick)
c5.place(x=406,y=256,width=74,height=74)
c6=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=c6_klick)
c6.place(x=481,y=256,width=74,height=74)
c7=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=c7_klick)
c7.place(x=556,y=256,width=74,height=74)
c8=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=c8_klick)
c8.place(x=631,y=256,width=74,height=74)
# D-Zeile
d1=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=d1_klick)
d1.place(x=106,y=331,width=74,height=74)
d2=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=d2_klick)
d2.place(x=181,y=331,width=74,height=74)
d3=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=d3_klick)
d3.place(x=256,y=331,width=74,height=74)
d4=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=d4_klick)
d4.place(x=331,y=331,width=74,height=74)
d5=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=d5_klick)
d5.place(x=406,y=331,width=74,height=74)
d6=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=d6_klick)
d6.place(x=481,y=331,width=74,height=74)
d7=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=d7_klick)
d7.place(x=556,y=331,width=74,height=74)
d8=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=d8_klick)
d8.place(x=631,y=331,width=74,height=74)
# E-Zeile
e1=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=e1_klick)
e1.place(x=106,y=406,width=74,height=74)
e2=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=e2_klick)
e2.place(x=181,y=406,width=74,height=74)
e3=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=e3_klick)
e3.place(x=256,y=406,width=74,height=74)
e4=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=e4_klick)
e4.place(x=331,y=406,width=74,height=74)
e5=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=e5_klick)
e5.place(x=406,y=406,width=74,height=74)
e6=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=e6_klick)
e6.place(x=481,y=406,width=74,height=74)
e7=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=e7_klick)
e7.place(x=556,y=406,width=74,height=74)
e8=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=e8_klick)
e8.place(x=631,y=406,width=74,height=74)
# F-Zeile
f1=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=f1_klick)
f1.place(x=106,y=481,width=74,height=74)
f2=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=f2_klick)
f2.place(x=181,y=481,width=74,height=74)
f3=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=f3_klick)
f3.place(x=256,y=481,width=74,height=74)
f4=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=f4_klick)
f4.place(x=331,y=481,width=74,height=74)
f5=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=f5_klick)
f5.place(x=406,y=481,width=74,height=74)
f6=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=f6_klick)
f6.place(x=481,y=481,width=74,height=74)
f7=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=f7_klick)
f7.place(x=556,y=481,width=74,height=74)
f8=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=f8_klick)
f8.place(x=631,y=481,width=74,height=74)
# G-Zeile
g1=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=g1_klick)
g1.place(x=106,y=556,width=74,height=74)
g2=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=g2_klick)
g2.place(x=181,y=556,width=74,height=74)
g3=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=g3_klick)
g3.place(x=256,y=556,width=74,height=74)
g4=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=g4_klick)
g4.place(x=331,y=556,width=74,height=74)
g5=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=g5_klick)
g5.place(x=406,y=556,width=74,height=74)
g6=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=g6_klick)
g6.place(x=481,y=556,width=74,height=74)
g7=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=g7_klick)
g7.place(x=556,y=556,width=74,height=74)
g8=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=g8_klick)
g8.place(x=631,y=556,width=74,height=74)
# H-Zeile
h1=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=h1_klick)
h1.place(x=106,y=631,width=74,height=74)
h2=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=h2_klick)
h2.place(x=181,y=631,width=74,height=74)
h3=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=h3_klick)
h3.place(x=256,y=631,width=74,height=74)
h4=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=h4_klick)
h4.place(x=331,y=631,width=74,height=74)
h5=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=h5_klick)
h5.place(x=406,y=631,width=74,height=74)
h6=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=h6_klick)
h6.place(x=481,y=631,width=74,height=74)
h7=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=h7_klick)
h7.place(x=556,y=631,width=74,height=74)
h8=Button(master=Haupt,bg="#245f11",activebackground="#40aa00",command=h8_klick)
h8.place(x=631,y=631,width=74,height=74)
# Aktivierung des Fensters
Haupt.mainloop()
stingray