При нажатии кнопки, выводить соответствующее изображение
При нажатии кнопки, выводить соответствующее изображение:
- Если победа - рядом с текстом ставится WIN.jpg
- Если проигрыш -- рядом с текстом ставится LOSE.jpg
- Если ничья- рядом с текстом ставится XZ.jpg.
Код:
from tkinter import *
import random as rdm
[![введите сюда описание изображения][2]][2]
class Main(Frame):
def __init__(self, root):
super(Main, self).__init__(root)
self.startUI()
def startUI(self):
btn = Button(root, text="Камень", font=("Times New Roman", 15),
command=lambda x=1: self.btn_click(x))
btn2 = Button(root, text="Ножницы", font=("Times New Roman", 15),
command=lambda x=2: self.btn_click(x))
btn3 = Button(root, text="Бумага", font=("Times New Roman", 15),
command=lambda x=3: self.btn_click(x))
btn.place(x=10, y=100, width=120, height=50)
btn2.place(x=155, y=100, width=120, height=50)
btn3.place(x=300, y=100, width=120, height=50)
self.lbl = Label(root, text="Начало игры!", bg="#FFF", font=("Times New Roman", 21, "bold"))
self.lbl.place(x=150, y=25)
self.win = self.drow = self.lose = 0
self.lbl2 = Label(root, justify="left", font=("Times New Roman", 13),
text=f"Побед: {self.win}\nПроигрышей:"
f" {self.lose}\nНичей: {self.drow}",
bg="#FFF")
self.lbl2.place(x=5, y=5)
def btn_click(self, choise):
comp_choise = rdm.randint(1, 3)
if choise == comp_choise:
self.drow += 1
self.lbl.configure(text="Ничья")
elif choise == 1 and comp_choise == 2 \
or choise == 2 and comp_choise == 3 \
or choise == 3 and comp_choise == 1:
self.win += 1
self.lbl.configure(text="Победа")
else:
self.lose += 1
self.lbl.configure(text="Проигрыш")
self.lbl2.configure(text=f"Побед: {self.win}\nПроигрышей:"
f" {self.lose}\nНичей: {self.drow}")
del comp_choise
if __name__ == '__main__':
root = Tk()
root.geometry("430x160+200+200")
root.title("Камень, ножницы, бумага")
root.resizable(False, False)
root["bg"] = "#FFF"
app = Main(root)
app.pack()
root.mainloop()
LOSE.jpg
WIN.jpg
XZ.jpg
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Попробуйте так:
from tkinter import *
import random as rdm
from PIL import ImageTk, Image
class Main(Frame):
def __init__(self, root):
super(Main, self).__init__(root)
self.startUI()
def startUI(self):
btn = Button(root, text="Камень", font=("Times New Roman", 15),
command=lambda x=1: self.btn_click(x))
btn2 = Button(root, text="Ножницы", font=("Times New Roman", 15),
command=lambda x=2: self.btn_click(x))
btn3 = Button(root, text="Бумага", font=("Times New Roman", 15),
command=lambda x=3: self.btn_click(x))
btn.place(x=10, y=100, width=120, height=50)
btn2.place(x=155, y=100, width=120, height=50)
btn3.place(x=300, y=100, width=120, height=50)
self.lbl = Label(root, text="Начало игры!", bg="#FFF", font=("Times New Roman", 21, "bold"))
self.lbl.place(x=150, y=25)
self.win = self.drow = self.lose = 0
self.lbl2 = Label(root, justify="left", font=("Times New Roman", 13),
text=f"Побед: {self.win}\nПроигрышей:"
f" {self.lose}\nНичей: {self.drow}",
bg="#FFF")
self.lbl2.place(x=5, y=5)
# +++
self.imgLabel = Label(root)
self.imgLabel.place(x=340, y=10, width=80, height=80)
def btn_click(self, choise):
comp_choise = rdm.randint(1, 3)
if choise == comp_choise:
self.drow += 1
self.lbl.configure(text="Ничья")
# +++
img = ImageTk.PhotoImage(Image.open("XZ.jpg"))
self.imgLabel.configure(image=img)
self.imgLabel.image = img
elif choise == 1 and comp_choise == 2 \
or choise == 2 and comp_choise == 3 \
or choise == 3 and comp_choise == 1:
self.win += 1
self.lbl.configure(text="Победа")
# +++
img = ImageTk.PhotoImage(Image.open("WIN.jpg"))
self.imgLabel.configure(image=img)
self.imgLabel.image = img
else:
self.lose += 1
self.lbl.configure(text="Проигрыш")
# +++
img = ImageTk.PhotoImage(Image.open("LOSE.jpg"))
self.imgLabel.configure(image=img)
self.imgLabel.image = img
self.lbl2.configure(text=f"Побед: {self.win}\nПроигрышей:"
f" {self.lose}\nНичей: {self.drow}")
del comp_choise
if __name__ == '__main__':
root = Tk()
root.geometry("430x160+200+200")
root.title("Камень, ножницы, бумага")
root.resizable(False, False)
root["bg"] = "#FFF"
app = Main(root)
app.pack()
root.mainloop()



