Python Tkinter-属性错误:';str';对象没有属性';设置';-掷骰子模拟器

Python Tkinter-属性错误:';str';对象没有属性';设置';-掷骰子模拟器,python,tkinter,dice,Python,Tkinter,Dice,下面是我正在尝试运行的代码。我试图通过小部件框获取用户输入,然后运行while循环来模拟掷骰子。我得到下面的错误 from tkinter import * from tkinter import ttk def Dice_roll(event): while True: x.get() if x[0].lower() == 'y': m = random.randrange(1, 7) if m ==

下面是我正在尝试运行的代码。我试图通过小部件框获取用户输入,然后运行while循环来模拟掷骰子。我得到下面的错误

from tkinter import *

from tkinter import ttk

def Dice_roll(event):

    while True:
        x.get()
        if x[0].lower() == 'y':
            m = random.randrange(1, 7)
            if m == 1:
                print("The number on the dice is", m)
            elif m == 2:
                print("The number on the dice is", m)
            elif m == 3:
                print("The number on the dice is", m)
            elif m == 4:
                print("The number on the dice is", m)
            elif m == 5:
                print("The number on the dice is", m)
            elif m == 6:
                print("The number on the dice is", m)
            else:pass

        if x[0].lower() == 'n':
           print("Thank you for playing the game :-)")
           break

x.delete(0, "end")
root = Tk()
x = Entry(root)
x.pack(side=LEFT)

Label(root, text="Want to roll the dice(Yes/No)?").pack(side=TOP)
Button.bind("<Button-1>",Dice_roll)
Button.pack(side=LEFT)

root.mainloop()
从tkinter导入*
从tkinter导入ttk
def骰子掷骰(事件):
尽管如此:
x、 得到()
如果x[0]。lower()=“y”:
m=随机。随机范围(1,7)
如果m==1:
打印(“骰子上的数字是”,m)
elif m==2:
打印(“骰子上的数字是”,m)
elif m==3:
打印(“骰子上的数字是”,m)
elif m==4:
打印(“骰子上的数字是”,m)
elif m==5:
打印(“骰子上的数字是”,m)
elif m==6:
打印(“骰子上的数字是”,m)
其他:通过
如果x[0].lower()=“n”:
打印(“感谢您玩这个游戏:-))
打破
x、 删除(0,“结束”)
root=Tk()
x=条目(根)
x、 包装(侧面=左侧)
标签(root,text=“想要掷骰子(是/否)?”)。包装(侧面=顶部)
按钮绑定(“,骰子滚动)
按钮包(侧面=左侧)
root.mainloop()
错误消息如下:我正在尝试从按钮1获取输入,该按钮需要传递给骰子滚动函数。这是我第一次尝试使用tkinter模块。我不确定该功能是否适用于字符串值

**AttributeError**              Traceback (most recent call last)  

<ipython-input-32-ce597da421bf> in <module>()
 45  
 46 Label(root, text="Want to roll the dice(Yes/No)?").pack(side=TOP)  
---> 47 Button.bind("<Button-1>",Dice_roll)  
 48 Button.pack(side=LEFT)  
 49  

~**\Continuum\anaconda3\lib\tkinter\__init__.py in bind**(self, sequence, func, add)  
   1243         of bound events are returned."""  
   1244  
-> 1245         return self._bind(('bind', self._w), sequence, func, add)  
   1246     def unbind(self, sequence, funcid=None):  
   1247         """Unbind for this widget for event SEQUENCE  the  

AttributeError: 'str' object has no attribute '_bind'
**AttributeError**回溯(最近一次调用)
在()
45
46标签(root,text=“想要掷骰子(是/否)?”)。包装(侧面=顶部)
--->47按钮。绑定(“,掷骰子)
48按钮组件(侧面=左侧)
49
绑定中的~**\Continuum\anaconda3\lib\tkinter\\uuuu init\uuuuuu.py**(self、sequence、func、add)
返回了1243个绑定事件。”“”
1244
->1245返回self.\u绑定(('bind',self.\u w),序列,函数,加法)
1246 def解除绑定(self、sequence、funcid=None):
1247“”为事件序列的此小部件解除绑定
AttributeError:'str'对象没有属性'\u bind'

您试图在
按钮
类本身上调用
绑定
,而没有实际创建
按钮
对象(相比之下,使用
标签
您首先正确创建了
标签
对象,然后在其上调用了
打包
)。首先需要构造一个
按钮
,然后对新对象调用
bind
。出现错误的原因是您试图调用该方法而没有将其绑定到对象,因此第一个位置参数(
“”
)被解释为
self
,当它试图调用其上的
按钮
方法时(在本例中,
set
),所有东西都坏了。

请修复问题中代码的缩进。很抱歉,这是我第二次在这里发帖。我也缩进了代码。我已经添加了在实现此代码时收到的错误消息,以及我试图用此代码实现的内容。如果目标不明确,请告诉我。我会提供更多的细节。谢谢你的回答。一旦我创建了按钮并调用了bind,就解决了这个错误。