V
V
Vasily Allin2019-09-18 22:24:31
Python
Vasily Allin, 2019-09-18 22:24:31

I can not understand why writes an error?

I am writing a program, but I ran into an error, I want that when I click on the button with the name "btn2" the Words1 and Words2 variables change their value. And at the same time, when you press the "btn" button, the Words1 variable randomly shuffles. Here is the whole code:

from tkinter import *
import time
import random

tk = Tk()
canvas = Canvas(tk, width=1000, height=800)
tk.resizable(0, 0)
my_background = PhotoImage(file=r'D:\\prog\\Darkand\\background.gif')
w = my_background.width()
h = my_background.height()
errors = 0
for x in range(0, 5):
    for y in range(0, 5):
        canvas.create_image(x * w, y * h, image=my_background, anchor='nw')
Words2 = ('Press a difficulty button')
Words1 = ('Press a difficulty button')


def Easy():
    Words2 = ('Огонь', 'Слово', 'Три', 'Молоко', 'Пять', 'Кошка')
    Words1 = ('огонь', 'слово', 'три', 'молоко', 'пять', 'кошка')
    pass

btn2 = Button(tk, text='Easy', command=Easy())
btn2.pack()
tk.update()


def words():
    random.shuffle(Words1)
    time.sleep(0.02)
    canvas.create_text(230, 600, text=Words1, font=('Helvetica', 20))
    Guess = str(input('Введи Слово 1:')).strip()
    if Guess == Words2[0] or Words1[0]:
        canvas.create_arc(700, 700, 50, 50)
        canvas.create_text(100, 300, text="Правильно!", font=('Helvetica', 20))
    else:
        canvas.create_text(100, 300, text="Неправильно!", font=('Helvetica', 20))
    Guess = str(input('Введи Слово 2:')).strip()
    if Guess == Words2[1] or Words1[1]:
        print('123')
        canvas.create_text(100, 300, text="Правильно!", font=('Helvetica', 20))
    else:
        canvas.create_text(100, 300, text="Неправильно!", font=('Helvetica', 20))
    Guess = str(input('Введи Слово 3:')).strip()
    if Guess == Words2[2] or Words1[2]:
        canvas.create_text(500, 300, text="Правильно!", font=('Helvetica', 20))
    else:
        canvas.create_text(100, 300, text="Неправильно!", font=('Helvetica', 20))
    Guess = str(input('Введи Слово 4:')).strip()
    if Guess == Words2[3] or Words1[3]:
        canvas.create_text(500, 540, text="Правильно!", font=('Helvetica', 20))
    else:
        canvas.create_text(100, 320, text="Неправильно!", font=('Helvetica', 20))
    Guess = str(input('Введи Слово 5:')).strip()
    imagethree = PhotoImage(file=r'D:\\prog\\Darkand\\three.gif')
    canvas.create_image(30, 500, image=imagethree, anchor='nw')
    if Guess == Words2[4] or Words1[4]:
        canvas.create_text(500, 540, text="Правильно!", font=('Helvetica', 20))
    else:
        canvas.create_text(100, 320, text="Неправильно!", font=('Helvetica', 20))
    Guess = str(input('Введи Слово 6:')).strip()
    if Guess == Words2[5] or Words1[5]:
        canvas.create_text(500, 540, text="Правильно!", font=('Helvetica', 20))
    else:
        canvas.create_text(100, 320, text="Неправильно!", font=('Helvetica', 20))


def clear():
    errors = 0
    canvas.delete("all")
    for x in range(0, 5):
        for y in range(0, 5):
            canvas.create_image(x * w, y * h, image=my_background, anchor='nw')
    canvas.pack()
    tk.update()


b = Button(tk, text="Clear", command=clear)
btn = Button(tk, text="Words", command=words)
canvas.pack()
btn.pack()
b.pack()
tk.update()
tk.mainloop()


def __init__():
    pass

It writes the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\prog\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "D:/ prog/Darkand/Darkandv2test.py", line 30, in words
random.shuffle(Words1)
File "D:\prog\lib\random.py", line 278, in shuffle
x[i], x[j] = x [j], x[i]
TypeError: 'str' object does not support item assignment

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey S., 2019-09-19
@Winsik

Words1 =['Огонь', 'Слово', 'Три', 'Молоко', 'Пять', 'Кошка']

;)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question