【问题标题】:AttributeError: module 'tkinter' has no attribute 'tk'AttributeError:模块“tkinter”没有属性“tk”
【发布时间】:2017-03-21 16:38:00
【问题描述】:

我正在尝试制作一个简单的音乐播放器,但我不断收到此错误:

Traceback (most recent call last):
    File "C:/Users/nickw/PycharmProjects/untitled1/music player", line 28, in <module>
        slider = tk.Scale(window, from_=100, to=0, command=setVolume)
      File "C:\Users\nickw\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2856, in __init__
        Widget.__init__(self, master, 'scale', cnf, kw)
      File "C:\Users\nickw\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2132, in __init__
        BaseWidget._setup(self, master, cnf)
      File "C:\Users\nickw\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2110, in _setup
        self.tk = master.tk
    AttributeError: module 'tkinter' has no attribute 'tk'

这是我的代码:

import pygame
import Tkinter as tk
window = tk.Tk()


pygame.init()
pygame.mixer.music.load("music")

started = False
playing = False

def buttonClick():
    global playing, started
    if not playing:
        if not started:
            pygame.mixer.music.play(-1)
            started=True
        else:
            pygame.mixer.music.unpause()
        button.config(text ="Pause")
    else:
        pygame.mixer.music.pause()
        button.config(text="play")
        playing = not playing
def setVolume(val):
    volume = float(slider.get())
    pygame.mixer.music.set_volume(volume /100)


slider = tk.Scale(window, text="play", command="buttonClick")
button = tk.Button(tk, text = "play", command = buttonClick)

slider.pack()
slider.set(100)
button.pack()
window.mainloop()

【问题讨论】:

  • Cannot find the error line slider = tk.Scale(window, from_=100, to=0, command=setVolume) in your code(在你的代码中看起来像这行 slider = tk.Scale(window, text="play", command="buttonClick")。另一个问题是在错误消息中,你使用的是 Python 3,但你使用了 @ 987654325@在你的代码中。应该是tkinter。发布你的真实代码。

标签: python tkinter python-3.5


【解决方案1】:

如果你已经将你的python文件名命名为tkinter.pyTkinter.py,将文件名改成别的,它会去掉attribute error

【讨论】:

    【解决方案2】:

    我居然发现了错误!有很多,所以tkinter 问题用

    解决了
    from tkinter import *
    tk=Tk()
    

    以及我用这条线纠正的比例问题。

    w = Scale(tk, from_=0, to=100, command=setVolume)
    

    【讨论】:

      【解决方案3】:

      如果您使用的是 python 3.x,则必须将 import-line 更改为

      import Tkinter as tk
      

      import tkinter as tk
      

      另一个问题是你的slider:构造函数需要一个函数作为最后一个参数,你给它一个字符串。您实际上知道正确的方法,如下行所示。

      【讨论】:

        猜你喜欢
        • 2021-11-03
        • 1970-01-01
        • 1970-01-01
        • 2018-11-18
        • 1970-01-01
        • 1970-01-01
        • 2015-09-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多