
teemo
VIP CRIMINAL
LV
11
- Awards
- 5
Merhaba, instagramda kod sayfalarında gezinirken "Are u dumb?" diye soru çıkaran bir kod gördüm ve no tuşuna bastığında tuşun yeri değişirken yes tuşuna basıldığında "biliyordum" tarzı bir mesaj çıkıyor. Bende bunu hem sıkıntıdan hem de tkinter ve random modülleri için örnek bir kod olur diye kodlamaya karar verdim.
Programın görünümü böyledir. Kodlar :
nesne yönelimi hali

Programın görünümü böyledir. Kodlar :
Python:
from tkinter import *
from random import choice
def Evet():
soru.destroy()
bildim = Label(text="Bunu biliyordum :3")
bildim.pack()
def Hayır():
xx = [250,230,270,290,280,240]
yy = [60,70,40,30,80]
hayir.place(x=choice(xx), y=choice(yy))
pencere = Tk()
soru = Label(text="Aptal mısın?")
evet = Button(text="Evet", command=Evet)
hayir = Button(text="Hayır", command=Hayır)
pencere.geometry("400x130")
evet.place(x=40,y=50,width=80)
hayir.place(x=260,y=50,width=80)
soru.pack()
pencere.mainloop()
nesne yönelimi hali
Python:
from tkinter import *
import random
class Root(Tk):
def __init__(self):
super().__init__()
self.soru = Label(text="Aptal mısın?")
self.soru.pack()
self.evet = Button(text="Evet", command=self.Evet)
self.hayir = Button(text="Hayır", command=self.Hayır)
self.geometry("400x130")
self.evet.place(x=40,y=50,width=80)
self.hayir.place(x=260,y=50,width=80)
self.mainloop()
def Evet(self):
self.soru.config(text="Bunu biliyordum! ")
def Hayır(self):
self.hayir.place(x=random.randint(0,400),y=random.randint(0,130))
Root(