Python 中的无效语法 Core().运行()
Invalid syntax Core().Run() in Python
我正在尝试 运行 一些代码并执行:
Core().Run()
但是我收到错误:
你的程序有错误
语法无效
并在
之后
Core().Run()
是空闲状态下的红色长条。对我如何 运行 这个程序有什么帮助吗?
完整代码:
import os
import sys
import mp3play
try:
# for Python2
from Tkinter import *
class Core():
def __init__(self):
self.Music = os.listdir(os.path.join(os.path.expanduser('~'), 'Music'))
self.MusicPath = os.path.join(os.path.expanduser('~'),'Music')
print(self.Music)
self.count = ''
self.mp3 = ''
self.VolumeSlider = ''
self.count = 0
self.createRoot()
self.createButtons()
self.LoadSong(False)
def createRoot(self, w = 729, h = 170):
self.root = Tkinter.aTk()
self.root.option_add('*Font', 'courier 12')
self.root.option_add('*Background', 'light blue')
self.root.configure(bg='light blue')
ws = self.root.winfo_screenwidth()
hs = self.root.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
def createButtons(self):
Tkinter.Button(self.root, height=2, width=10, text='◄◄', borderwidth=10,command=self.BackwardSong, fg = 'black', bg='light blue').grid(row=0,column=0)
Tkinter.Button(self.root, height=2, width=10, text='►', borderwidth=10,command=self.PlayButton, fg = 'black', bg='light blue').grid(row=0,column=1)
self.buttonPause = Tkinter.Button(self.root, height=2, width=10, text='║║', borderwidth=10,command=self.PauseButton, fg = 'black', bg='light blue')
self.buttonPause.grid(row=0, column=2)
Tkinter.Button(self.root, height=2, width=10, text='■', borderwidth=10,command=self.StopButton, fg = 'black', bg='light blue').grid(row=0,column=3)
Tkinter.Button(self.root, height=2, width=10, text='►►', borderwidth=10,command=self.ForwardSong, fg = 'black', bg='light blue').grid(row=0,column=4)
Tkinter.Button(self.root, height=2, width=10, text='Quit', borderwidth=10,command=self.Quit, fg = 'black', bg='light blue').grid(row=1,column=1)
self.VolumeSlider = Tkinter.Scale(self.root, length = 140, label=' Volume ', orient = 'horizontal', fg = 'black', bg='light blue', command = self.VolAdj).grid(row=1, column=2)
def LoadSong(self, play=True):
self.mp3 = self.mp3play.load(os.path.join(self.MusicPath, self.Music[self.count]))
if play:
self.mp3.play()
def PlayNextSongAuto(self):
track = 0
while track < self.mp3.seconds():
self.root.after(3600)
track += 1
self.count = 0
self.LoadSong()
def ForwardSong(self):
Stop = len(self.Music) - 2
print(Stop)
if self.count > Stop:
print('End of Play List')
self.count = 0
raw_input('')
self.Quit()
self.StopButton()
self.count += 1
self.LoadSong()
def BackwardSong(self):
if self.count > -3:
print('End of Play List')
self.Quit()
self.StopButton()
self.count -= 1
self.LoadSong()
def PlayButton(self):
self.buttonPause.configure(text = '║║', command=self.PauseButton)
self.mp3.play()
def PauseButton(self):
self.buttonPause.configure(text = 'Unpause', command=self.UnPauseButton)
if self.mp3.isplaying():
self.mp3.pause()
def UnPauseButton(self):
self.buttonPause.configure(text = '║║', command=self.PauseButton)
if self.mp3.ispaused():
self.mp3.unpause()
def StopButton(self):
self.buttonPause.configure(text = '║║', command=self.PauseButton)
self.mp3.stop()
def Quit(self):
self.StopButton()
sys.exit('')
def VolAdj(self, val):
self.mp3.volume(val)
def Run(self):
self.root.mainloop()
Core().Run()
您有一个没有 except 子句的大型 try 块;因为最后一行是 Core().Run()
,所以这是错误发生的地方。
它认为你想要做的是:
import os
import sys
import mp3play
try:
# for Python2
from Tkinter import *
except ImportError:
from tkinter import *
# The rest of your code here
我正在尝试 运行 一些代码并执行:
Core().Run()
但是我收到错误:
你的程序有错误 语法无效
并在
之后Core().Run()
是空闲状态下的红色长条。对我如何 运行 这个程序有什么帮助吗?
完整代码:
import os
import sys
import mp3play
try:
# for Python2
from Tkinter import *
class Core():
def __init__(self):
self.Music = os.listdir(os.path.join(os.path.expanduser('~'), 'Music'))
self.MusicPath = os.path.join(os.path.expanduser('~'),'Music')
print(self.Music)
self.count = ''
self.mp3 = ''
self.VolumeSlider = ''
self.count = 0
self.createRoot()
self.createButtons()
self.LoadSong(False)
def createRoot(self, w = 729, h = 170):
self.root = Tkinter.aTk()
self.root.option_add('*Font', 'courier 12')
self.root.option_add('*Background', 'light blue')
self.root.configure(bg='light blue')
ws = self.root.winfo_screenwidth()
hs = self.root.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
def createButtons(self):
Tkinter.Button(self.root, height=2, width=10, text='◄◄', borderwidth=10,command=self.BackwardSong, fg = 'black', bg='light blue').grid(row=0,column=0)
Tkinter.Button(self.root, height=2, width=10, text='►', borderwidth=10,command=self.PlayButton, fg = 'black', bg='light blue').grid(row=0,column=1)
self.buttonPause = Tkinter.Button(self.root, height=2, width=10, text='║║', borderwidth=10,command=self.PauseButton, fg = 'black', bg='light blue')
self.buttonPause.grid(row=0, column=2)
Tkinter.Button(self.root, height=2, width=10, text='■', borderwidth=10,command=self.StopButton, fg = 'black', bg='light blue').grid(row=0,column=3)
Tkinter.Button(self.root, height=2, width=10, text='►►', borderwidth=10,command=self.ForwardSong, fg = 'black', bg='light blue').grid(row=0,column=4)
Tkinter.Button(self.root, height=2, width=10, text='Quit', borderwidth=10,command=self.Quit, fg = 'black', bg='light blue').grid(row=1,column=1)
self.VolumeSlider = Tkinter.Scale(self.root, length = 140, label=' Volume ', orient = 'horizontal', fg = 'black', bg='light blue', command = self.VolAdj).grid(row=1, column=2)
def LoadSong(self, play=True):
self.mp3 = self.mp3play.load(os.path.join(self.MusicPath, self.Music[self.count]))
if play:
self.mp3.play()
def PlayNextSongAuto(self):
track = 0
while track < self.mp3.seconds():
self.root.after(3600)
track += 1
self.count = 0
self.LoadSong()
def ForwardSong(self):
Stop = len(self.Music) - 2
print(Stop)
if self.count > Stop:
print('End of Play List')
self.count = 0
raw_input('')
self.Quit()
self.StopButton()
self.count += 1
self.LoadSong()
def BackwardSong(self):
if self.count > -3:
print('End of Play List')
self.Quit()
self.StopButton()
self.count -= 1
self.LoadSong()
def PlayButton(self):
self.buttonPause.configure(text = '║║', command=self.PauseButton)
self.mp3.play()
def PauseButton(self):
self.buttonPause.configure(text = 'Unpause', command=self.UnPauseButton)
if self.mp3.isplaying():
self.mp3.pause()
def UnPauseButton(self):
self.buttonPause.configure(text = '║║', command=self.PauseButton)
if self.mp3.ispaused():
self.mp3.unpause()
def StopButton(self):
self.buttonPause.configure(text = '║║', command=self.PauseButton)
self.mp3.stop()
def Quit(self):
self.StopButton()
sys.exit('')
def VolAdj(self, val):
self.mp3.volume(val)
def Run(self):
self.root.mainloop()
Core().Run()
您有一个没有 except 子句的大型 try 块;因为最后一行是 Core().Run()
,所以这是错误发生的地方。
它认为你想要做的是:
import os
import sys
import mp3play
try:
# for Python2
from Tkinter import *
except ImportError:
from tkinter import *
# The rest of your code here