[英]AttributeError: module 'tkinter' has no attribute 'tk'
I am trying to make a simple music player but I keep getting this error:我正在尝试制作一个简单的音乐播放器,但我不断收到此错误:
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'
This is my code:这是我的代码:
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()
If you are using python 3.x you have to change your import
-line from如果您在使用Python 3.x的,你必须改变你的
import
从直插
import Tkinter as tk
to到
import tkinter as tk
Another problem is your slider
: the constructor expects a function as last argument, you give it a string.另一个问题是你的
slider
:构造函数需要一个函数作为最后一个参数,你给它一个字符串。 You actually know the correct way, as I can see on the following line.您实际上知道正确的方法,正如我在以下行中看到的那样。
I actually found the error!我实际上发现了错误! There was many so the
tkinter
problem was solved with有很多所以
tkinter
问题解决了
from tkinter import *
tk=Tk()
and the scale problem I corrected with this line.以及我用这条线纠正的比例问题。
w = Scale(tk, from_=0, to=100, command=setVolume)
如果您将python文件名命名为tkinter.py
或Tkinter.py
,请将文件名更改为其他名称,它将摆脱attribute error
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.