plarmi

RSP tkinter

Apr 15th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. from tkinter import *
  2. import random as rdm
  3.  
  4.  
  5. class Main(Frame):
  6.     def __init__(self, root):
  7.         super(Main, self).__init__(root)
  8.         self.startUI()
  9.  
  10.     def startUI(self):
  11.         btn = Button(root, text="Камень", font=("Times New Roman", 15),
  12.                      command=lambda x=1: self.btn_click(x))
  13.         btn2 = Button(root, text="Ножницы", font=("Times New Roman", 15),
  14.                       command=lambda x=2: self.btn_click(x))
  15.         btn3 = Button(root, text="Бумага", font=("Times New Roman", 15),
  16.                       command=lambda x=3: self.btn_click(x))
  17.  
  18.         btn.place(x=10, y=100, width=120, height=50)
  19.         btn2.place(x=155, y=100, width=120, height=50)
  20.         btn3.place(x=300, y=100, width=120, height=50)
  21.  
  22.         self.lbl = Label(root, text="Начало игры!", bg="#FFF", font=("Times New Roman", 21, "bold"))
  23.         self.lbl.place(x=150, y=25)
  24.  
  25.         self.win = self.drow = self.lose = 0
  26.  
  27.         self.lbl2 = Label(root, justify="left", font=("Times New Roman", 13),
  28.                          text=f"Побед: {self.win}\nПроигрышей:"
  29.                               f" {self.lose}\nНичей: {self.drow}",
  30.                          bg="#FFF")
  31.         self.lbl2.place(x=5, y=5)
  32.  
  33.     def btn_click(self, choise):
  34.         comp_choise = rdm.randint(1, 3)
  35.  
  36.         if choise == comp_choise:
  37.             self.drow += 1
  38.             self.lbl.configure(text="Ничья")
  39.         elif choise == 1 and comp_choise == 2 \
  40.                 or choise == 2 and comp_choise == 3 \
  41.                 or choise == 3 and comp_choise == 1:
  42.             self.win += 1
  43.             self.lbl.configure(text="Победа")
  44.         else:
  45.             self.lose += 1
  46.             self.lbl.configure(text="Проигрыш")
  47.  
  48.         self.lbl2.configure(text=f"Побед: {self.win}\nПроигрышей:"
  49.                               f" {self.lose}\nНичей: {self.drow}")
  50.  
  51.         del comp_choise
  52.  
  53.  
  54. if __name__ == '__main__':
  55.     root = Tk()
  56.     root.geometry("430x160+200+200")
  57.     root.title("Камень, ножницы, бумага")
  58.     root.resizable(False, False)
  59.     root["bg"] = "#FFF"
  60.     app = Main(root)
  61.     app.pack()
  62.     root.mainloop()
  63.  
Add Comment
Please, Sign In to add comment