简体   繁体   English

AttributeError:'str'对象没有属性'set'

[英]AttributeError: 'str' object has no attribute 'set'

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. 游戏结束后,将调用gameover()函数,并且必须在另一个窗口上显示标签(祝贺消息,即玩家姓名获胜!)。 I am trying to achieve this by using a format operator, set() and get() functions. 我试图通过使用格式运算符,set()和get()函数来实现这一点。

Error: 错误:

Traceback (most recent call last): 追溯(最近一次通话):
File "C:\\Users\\a\\AppData\\Local\\Programs\\Python\\Python37-32\\ZPrograms\\XOXO.py", line 59, in 文件“ C:\\ Users \\ a \\ AppData \\ Local \\ Programs \\ Python \\ Python37-32 \\ ZPrograms \\ XOXO.py”,第59行,在
playerX=winplayX.set() playerX = winplayX.set()
AttributeError: 'str' object has no attribute 'set' AttributeError:'str'对象没有属性'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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM