This is a simple Tic-Tac-Toe game. When the game is completed, the function gameover()
is called and a label (congratulatory message ie playername wins!) has to be displayed on another window. I am trying to achieve this by using a format operator, set() and get() functions.
Error:
Traceback (most recent call last):
File "C:\\Users\\a\\AppData\\Local\\Programs\\Python\\Python37-32\\ZPrograms\\XOXO.py", line 59, in
playerX=winplayX.set()
AttributeError: 'str' object has no attribute 'set'
How do I solve this?
Code:
playerX = StringVar()
playerY = StringVar()
winplayX = StringVar()
winplayY = StringVar()
winplayX= playerX.get()
winplayY= playerY.get()
playerX=winplayX.set()
playerY=winplayY.set()
u=Frame(root)
u.grid(row=1)
playerX_name = Label( u, text="Player X:", font='Times 25 bold', bg='white', fg='black', height=1, width=10)
playerX_name.grid(row=1, column=2)
playerX_name=Entry(u,textvariable=playerX,font="Times 25")
playerX_name.grid(row=1, column=3)
v=Frame(root)
v.grid(row=2)
playerO_name = Label( v, text="Player O:", font='Times 25 bold', bg='white', fg='black', height=1, width=10)
playerO_name.grid(row=2, column=0)
playerO_name = Entry(v,textvariable=playerY,font="Times 25")
playerO_name.grid(row=2, column=3)
def gameover(winflag):
win= Toplevel()
win.title("Game Over")
win.geometry("260x260+614+339")
global winplayX
global winplayY
if winflag==1:
wLabel1=Label(win, text="%s Wins!"%winplayX,font='Times 20 bold').place(x=80, y=80)
elif winflag==2:
wLabel2=Label(win, text="%s Wins!"%winplayY,font='Times 20 bold').place(x=80, y=80)
elif winflag==3:
wLabel3=Label(win, text="It is a tie",font='Times 20 bold').place(x=80, y=80)
If what you are trying to do, is just have the player names be outputted. You can just change this code:
playerX = StringVar()
playerY = StringVar()
winplayX = StringVar()
winplayY = StringVar()
winplayX= playerX.get()
winplayY= playerY.get()
playerX=winplayX.set()
playerY=winplayY.set()
to this:
winplayX = StringVar()
winplayY = StringVar()
winplayX.set("Player1")
winplayY.set("Player2")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.