Python - AttributeError: 'MyLogger' object has no attribute 'ui'
Python - AttributeError: 'MyLogger' object has no attribute 'ui'
我是 python 和对象的新手,抱歉,我需要帮助。 :)
当我 运行 我的代码出现此错误时:"Python - AttributeError: 'MyLogger' object has no attribute 'ui'".
因为我用了两个类?
因为我可以更改进度条的值?
这是我的代码:
from __future__ import unicode_literals
from Musicdl import *
import sys, youtube_dl, re, threading
class Musicdl(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Window()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.button_download, QtCore.SIGNAL('clicked()'), start_thread)
class MyLogger(object):
def debug(self, msg):
get_progressvalue(self, msg)
def warning(self, msg):
pass
def error(self, msg):
pass
def start_thread():
t = threading.Thread(target=download)
t.start()
def my_hook(d):
if d['status'] == 'finished':
print('Descarga completada. Convirtiendo ...')
ydl_opts = {
"progress_with_newline": True,
"outtmpl": "%(title)s.%(ext)s",
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '320',
}],
'logger': MyLogger(),
'progress_hooks': [my_hook],
}
progressvalue = int(0)
def get_progressvalue(self, msg):
global progressvalue
match = re.match(r'\[\w+\]\s+(\d{1,3})', msg)
if match:
value = int(match.group(1))
if value != progressvalue:
progressvalue = value
print(progressvalue)
self.ui.progressbar.setValue(10)
def download():
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['...'])
print('Terminado.')
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = Musicdl()
myapp.show()
sys.exit(app.exec_())
抱歉写了所有代码,但我不知道该怎么做。
非常感谢!!
我怀疑 get_progressvalue()
是用第一个参数作为记录器调用的。
self.ui.progressbar.setValue(10)
这是产生错误的行(在 MyLogger
内)
def debug(self, msg):
get_progressvalue(self, msg)
我怀疑 get_progressvalue 中的 self 必须替换为 Musicdl 对象的 self 而不是 MyLogger 的。
看到你的评论,你应该做的是将 Musicdls self 传递给 myLoggers 初始化程序。更好的方法是只传入 musicdls ui 属性。
class MyLogger(object):
def __init__(self, musicdl=None):
self.musicdl = musicdl
def debug(self, msg):
get_progressvalue(self.musicdl, msg)
def warning(self, msg):
pass
def error(self, msg):
pass
这不是最佳解决方案,但要使其正常工作,您必须将 "if name main loop" 中的 ydl_opts["logger"]
条目更改为 MyLogger(musicdl=myapp)
。
我是 python 和对象的新手,抱歉,我需要帮助。 :) 当我 运行 我的代码出现此错误时:"Python - AttributeError: 'MyLogger' object has no attribute 'ui'".
因为我用了两个类? 因为我可以更改进度条的值?
这是我的代码:
from __future__ import unicode_literals
from Musicdl import *
import sys, youtube_dl, re, threading
class Musicdl(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Window()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.button_download, QtCore.SIGNAL('clicked()'), start_thread)
class MyLogger(object):
def debug(self, msg):
get_progressvalue(self, msg)
def warning(self, msg):
pass
def error(self, msg):
pass
def start_thread():
t = threading.Thread(target=download)
t.start()
def my_hook(d):
if d['status'] == 'finished':
print('Descarga completada. Convirtiendo ...')
ydl_opts = {
"progress_with_newline": True,
"outtmpl": "%(title)s.%(ext)s",
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '320',
}],
'logger': MyLogger(),
'progress_hooks': [my_hook],
}
progressvalue = int(0)
def get_progressvalue(self, msg):
global progressvalue
match = re.match(r'\[\w+\]\s+(\d{1,3})', msg)
if match:
value = int(match.group(1))
if value != progressvalue:
progressvalue = value
print(progressvalue)
self.ui.progressbar.setValue(10)
def download():
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['...'])
print('Terminado.')
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = Musicdl()
myapp.show()
sys.exit(app.exec_())
抱歉写了所有代码,但我不知道该怎么做。
非常感谢!!
我怀疑 get_progressvalue()
是用第一个参数作为记录器调用的。
self.ui.progressbar.setValue(10)
这是产生错误的行(在 MyLogger
内)
def debug(self, msg):
get_progressvalue(self, msg)
我怀疑 get_progressvalue 中的 self 必须替换为 Musicdl 对象的 self 而不是 MyLogger 的。
看到你的评论,你应该做的是将 Musicdls self 传递给 myLoggers 初始化程序。更好的方法是只传入 musicdls ui 属性。
class MyLogger(object):
def __init__(self, musicdl=None):
self.musicdl = musicdl
def debug(self, msg):
get_progressvalue(self.musicdl, msg)
def warning(self, msg):
pass
def error(self, msg):
pass
这不是最佳解决方案,但要使其正常工作,您必须将 "if name main loop" 中的 ydl_opts["logger"]
条目更改为 MyLogger(musicdl=myapp)
。