Vershitel_sudeb

Крестики - нолики на tkinter

Mar 24th, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.51 KB | None | 0 0
  1. from tkinter import *
  2.  
  3. flag = False
  4. winner = False
  5.  
  6.  
  7. def hod():
  8.     global flag
  9.     flag = not flag
  10.     if flag:
  11.         return 'x'
  12.     else:
  13.         return 'o'
  14.  
  15.  
  16. def f():
  17.  
  18.     xo = Tk()
  19.     but = Button(xo, text=" ", width=10, height=5, font="Arial 16",
  20.                  bg="White", fg="Black", command=lambda: p(but))
  21.  
  22.     but2 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
  23.                   bg="White", fg="Black", command=lambda: p(but2))
  24.  
  25.     but3 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
  26.                   bg="White", fg="Black", command=lambda: p(but3))
  27.  
  28.     but4 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
  29.                   bg="White", fg="Black", command=lambda: p(but4))
  30.  
  31.     but5 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
  32.                   bg="White", fg="Black", command=lambda: p(but5))
  33.  
  34.     but6 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
  35.                   bg="White", fg="Black", command=lambda: p(but6))
  36.  
  37.     but7 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
  38.                   bg="White", fg="Black", command=lambda: p(but7))
  39.  
  40.     but8 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
  41.                   bg="White", fg="Black", command=lambda: p(but8))
  42.  
  43.     but9 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
  44.                   bg="White", fg="Black", command=lambda: p(but9))
  45.  
  46.     but.grid(row=1, column=1)
  47.     but2.grid(row=1, column=2)
  48.     but3.grid(row=1, column=3)
  49.     but4.grid(row=2, column=1)
  50.     but5.grid(row=2, column=2)
  51.     but6.grid(row=2, column=3)
  52.     but7.grid(row=3, column=1)
  53.     but8.grid(row=3, column=2)
  54.     but9.grid(row=3, column=3)
  55.  
  56.     def p(button):
  57.         if button["text"] == " " and not winner:
  58.             button["text"] = hod()
  59.             win()
  60.  
  61.     def win():
  62.         global winner
  63.  
  64.         if but['text'] == but2['text'] == but3['text'] and but['text'] != ' ':
  65.             but['bg'] = 'red'
  66.             but2['bg'] = 'red'
  67.             but3['bg'] = 'red'
  68.             winner = True
  69.  
  70.         if but4['text'] == but5['text'] == but6['text'] and but4['text'] != ' ':
  71.             but4['bg'] = 'red'
  72.             but5['bg'] = 'red'
  73.             but6['bg'] = 'red'
  74.             winner = True
  75.  
  76.         if but7['text'] == but8['text'] == but9['text'] and but7['text'] != ' ':
  77.             but7['bg'] = 'red'
  78.             but8['bg'] = 'red'
  79.             but9['bg'] = 'red'
  80.             winner = True
  81.  
  82.         if but['text'] == but4['text'] == but7['text'] and but['text'] != ' ':
  83.             but['bg'] = 'red'
  84.             but4['bg'] = 'red'
  85.             but7['bg'] = 'red'
  86.             winner = True
  87.  
  88.         if but2['text'] == but5['text'] == but8['text'] and but2['text'] != ' ':
  89.             but2['bg'] = 'red'
  90.             but5['bg'] = 'red'
  91.             but8['bg'] = 'red'
  92.             winner = True
  93.  
  94.         if but3['text'] == but6['text'] == but9['text'] and but3['text'] != ' ':
  95.             but3['bg'] = 'red'
  96.             but6['bg'] = 'red'
  97.             but9['bg'] = 'red'
  98.             winner = True
  99.  
  100.         if but['text'] == but5['text'] == but9['text'] and but['text'] != ' ':
  101.             but['bg'] = 'red'
  102.             but5['bg'] = 'red'
  103.             but9['bg'] = 'red'
  104.             winner = True
  105.  
  106.         if but3['text'] == but5['text'] == but7['text'] and but3['text'] != ' ':
  107.             but3['bg'] = 'red'
  108.             but5['bg'] = 'red'
  109.             but7['bg'] = 'red'
  110.             winner = True
  111.  
  112.     xo.mainloop()
  113.  
  114.  
  115. f()
  116.  
Add Comment
Please, Sign In to add comment