Pythonda label içindeki textin kontrolü

Le Chiffre

Decapat
Katılım
13 Eylül 2021
Mesajlar
109
Çözümler
1
Merhabalar, pythonda tkinter ile x-o-x oyunu yapmaya çalışıyorum. Belli bir yere kadar geldim takıldığım kısım ise labelların text kısmını if else ile kontrol edemiyorum. Örneğin alt alta 3 kutu x olursa kazandın yazısı çıkartmak istiyorum. Yardımcı olursanız sevinirim.

from tkinter import*
import tkinter as tk
def tum_fonksiyonlar():

def label1_fonk():
label1 = tk.Label(pencere1, text="X", bg="light green", height=8, width=17)
label1.place(x=0, y=0)

def label2_fonk():
label1 = tk.Label(pencere1, text="O", bg="light green", height=8, width=17)
label1.place(x=0, y=0)

def label3_fonk():
label2=tk.Label(pencere1,text="X",bg="light green", height=8, width=17)
label2.place(x=0, y=134)

def label4_fonk():
label2=tk.Label(pencere1,text="O", bg="light green", height=8, width=17)
label2.place(x=0, y=134)

def label5_fonk():
label3=tk.Label(pencere1,text="X", bg="light green", height=8, width=17)
label3.place(x=0, y=268)

def label6_fonk():
label3=tk.Label(pencere1,text="O", bg="light green", height=8, width=17)
label3.place(x=0, y=268)

def label7_fonk():
label4=tk.Label(pencere1,text="X", bg="light green", height=8, width=17)
label4.place(x=134,y=0)

def label8_fonk():
label4=tk.Label(pencere1,text="O", bg="light green", height=8, width=17)
label4.place(x=134,y=0)

def label9_fonk():
label5=tk.Label(pencere1,text="X", bg="light green", height=8, width=17)
label5.place(x=134,y=134)

def label10_fonk():
label5=tk.Label(pencere1,text="O", bg="light green", height=8, width=17)
label5.place(x=134,y=134)

def label11_fonk():
label6=tk.Label(pencere1,text="X", bg="light green", height=8, width=17)
label6.place(x=134,y=268)

def label12_fonk():
label6=tk.Label(pencere1,text="O", bg="light green", height=8, width=17)
label6.place(x=134,y=268)

def label13_fonk():
label7=tk.Label(pencere1,text="X", bg="light green", height=8, width=17)
label7.place(x=268,y=0)

def label14_fonk():
label7=tk.Label(pencere1,text="O", bg="light green", height=8, width=17)
label7.place(x=268,y=0)

def label15_fonk():
label8=tk.Label(pencere1,text="X", bg="light green", height=8, width=17)
label8.place(x=268,y=134)

def label16_fonk():
label8=tk.Label(pencere1,text="O", bg="light green", height=8, width=17)
label8.place(x=268,y=134)

def label17_fonk():
label9=tk.Label(pencere1,text="X", bg="light green", height=8, width=17)
label9.place(x=268,y=268)

def label18_fonk():
label9=tk.Label(text="O", bg="light green", height=8, width=17)
label9.place(x=268,y=268)

pencere1 =tk.Tk()
pencere1.geometry("400x400")
pencere1.title("X-O-X")

canvas1=Canvas(pencere1, width=400, height=400)

buton1=Button(pencere1,height=8,width=17,bg="light blue")
buton1.place(x=0,y=0)
buton1_yeni = Button(pencere1, text="X", bg="red", height=2, width=4, command=label1_fonk)
buton1_yeni.place(x=20, y=40)
buton1_yeni = Button(pencere1, text="O", bg="red", height=2, width=4, command=label2_fonk)
buton1_yeni.place(x=70, y=40)

buton2=Button(pencere1,height=8,width=17,bg="light blue")
buton2.place(x=0,y=134)
buton2_yeni = tk.Button(pencere1, text="X", bg="red", height=2, width=4, command=label3_fonk)
buton2_yeni.place(x=20, y=175)
buton22_yeni = tk.Button(pencere1, text="O", bg="red", height=2, width=4, command=label4_fonk)
buton22_yeni.place(x=70, y=175)

buton3=Button(pencere1,height=8,width=17,bg="light blue")
buton3.place(x=0,y=268)
buton3_yeni = tk.Button(pencere1, text="X", bg="red", height=2, width=4, command=label5_fonk)
buton3_yeni.place(x=20, y=310)
buton33_yeni = tk.Button(pencere1, text="O", bg="red", height=2, width=4, command=label6_fonk)
buton33_yeni.place(x=70, y=310)

buton4=Button(pencere1,height=8,width=17,bg="light blue")
buton4.place(x=134,y=0)
buton4_yeni = tk.Button(pencere1, text="X", bg="red", height=2, width=4, command=label7_fonk)
buton4_yeni.place(x=155, y=40)
buton44_yeni = tk.Button(pencere1, text="O", bg="red", height=2, width=4, command=label8_fonk)
buton44_yeni.place(x=205, y=40)

buton5=Button(pencere1,height=8,width=17,bg="light blue")
buton5.place(x=134,y=134)
buton5_yeni = tk.Button(pencere1, text="X", bg="red", height=2, width=4, command=label9_fonk)
buton5_yeni.place(x=155, y=175)
buton55_yeni = tk.Button(pencere1, text="O", bg="red", height=2, width=4, command=label10_fonk)
buton55_yeni.place(x=205, y=175)

buton6=Button(pencere1,height=8,width=17,bg="light blue")
buton6.place(x=134,y=268)
buton6_yeni = tk.Button(pencere1, text="X", bg="red", height=2, width=4, command=label11_fonk)
buton6_yeni.place(x=155, y=310)
buton66_yeni = tk.Button(pencere1, text="O", bg="red", height=2, width=4, command=label12_fonk)
buton66_yeni.place(x=205, y=310)

buton7=Button(pencere1,height=8,width=17,bg="light blue")
buton7.place(x=268,y=0)
buton7_yeni = tk.Button(pencere1, text="X", bg="red", height=2, width=4, command=label13_fonk)
buton7_yeni.place(x=290, y=40)
buton77_yeni = tk.Button(pencere1, text="O", bg="red", height=2, width=4, command=label14_fonk)
buton77_yeni.place(x=349, y=40)

buton8=Button(pencere1,height=8,width=17,bg="light blue")
buton8.place(x=268,y=134)
buton8_yeni = tk.Button(pencere1, text="X", bg="red", height=2, width=4, command=label15_fonk)
buton8_yeni.place(x=290, y=175)
buton88_yeni = tk.Button(pencere1, text="O", bg="red", height=2, width=4, command=label16_fonk)
buton88_yeni.place(x=340, y=175)

buton9=Button(pencere1,height=8,width=17,bg="light blue")
buton9.place(x=268,y=268)
buton9_yeni = tk.Button(pencere1, text="X", bg="red", height=2, width=4, command=label17_fonk)
buton9_yeni.place(x=290, y=310)
buton99_yeni = tk.Button(pencere1, text="O", bg="red", height=2, width=4, command=label18_fonk)
buton99_yeni.place(x=340, y=310)

canvas1.create_line(130,0,130,400, fill="salmon", width=8)
canvas1.create_line(265,0,265,400, fill="salmon",width=8)
canvas1.create_line(0,130,400,130, fill="salmon",width=8)
canvas1.create_line(0,265,400,265, fill="salmon",width=8)
canvas1.pack()

mainloop()


print(tum_fonksiyonlar())



1.PNG
2.PNG
 
Şu koddaki yazım durumunun dışına çıkmak istemiyorsan, label fonk içlerinde label durum gibi bir değer ata, X için 0 olsun y için 1 olsun.
Senaryolar oluştur örneğin L1 = 0 ve L2 = 0 ve L3 = 0'sa X kazansın gibi. Tabii bu fikir şu durumdaki bir kod için, sanırım çok daha iyi şekilde yazılabilir.
 
Son düzenleme:
Şu koddaki yazım durumunun dışına çıkmak istemiyorsan, label fonk içlerinde label durum gibi bir değer ata, X için 0 olsun y için 1 olsun.
Senaryolar oluştur örneğin L1 = 0 ve L2 = 0 ve L3 = 0'sa X kazansın gibi. Tabii bu fikir şu durumdaki bir kod için, sanırım çok daha iyi şekilde yazılabilir.
Öncelikle teşekkür ederim. Ne demek istediğinizi anladım fakat fonksiyon içinde yazdığım L'leri sadece o fonksiyon içinde karşılaştırabiliyorum. Fonksiyon dışında karşılaştırmak için global kullandım bu seferde ilk verdiğim değerlere göre karşılaştırıyor.
 
En başta değerleri None olarak tanımlayabilirsiniz. Global ile fonksiyon içine alırsınız. Böylece karşılaştırmada etkisi olmaz.

Python:
a = None

def fonk():
    global a
    a = 1
    return a

if a == 1:
    print("1")
Python:
a = None

def fonk():
    global a
    a = 1
    return a

fonk()

if a == 1:
    print("1")
 
Toplam 18 tane label fonksiyonu belirttim. Bu yüzden labelların durumlarını karşılaştıramıyorum. Hepsi tek bir fonksiyon içinde olduğu zaman da tek bir kutucukta seçtiğim seçenek doğal olarak tüm kutucuklarda seçilmiş oluyor. Umarım anlatabilmişimdir.

En başta değerleri None olarak tanımlayabilirsiniz. Global ile fonksiyon içine alırsınız. Böylece karşılaştırmada etkisi olmaz.

Python:
a = None

def fonk():
    global a
    a = 1
    return a

if a == 1:
    print("1")
Python:
a = None

def fonk():
    global a
    a = 1
    return a

fonk()

if a == 1:
    print("1")

Bu sefer kod çalışıyor ama fonksiyonu çağırdığımızdan kodu başlatınca kendisi otomatik seçiyor.
 

Dosya Ekleri

  • 3.PNG
    3.PNG
    42 KB · Görüntüleme: 16
Son düzenleme:
Fonkisyonu buttonclickde çağıracaksın çünkü.
Çağırdığın fonksiyonları sil sadece. 1 ve 2'de aynı şeye tıkla çıktıyı görürsün. Tabii o sorgulamayı da bir fonksiyonun içine alıp label fonksiyonlarında çalıştırmalısın.
 

Yeni konular

Geri
Yukarı