class xDialog(QDialog): def __init__(self, parent=None): super(xDialog, self).__init__(parent) self.ui = Ui_Form() self.ui.setupUi(self) self.ui.btnOne.clicked.connect(self.anyBtn) self.ui.btnTwo.clicked.connect(self.anyBtn) self.ui.btnThree.clicked.connect(self.anyBtn) def anyBtn(self): button = self.sender() if button is None or not isinstance(button, QPushButton): return self.ui.labelInfo.setText("You clicked button %s" % button.text())
class WordGenerator(QWidget): def __init__(self, parent=None): super(WordGenerator, self).__init__(parent) self.ui = Ui_Form() self.ui.setupUi(self) self.ui.retranslateUi(self) self.Wort = '' self.setAcceptDrops(True) QtCore.QObject.connect(self.ui.NextLetter, QtCore.SIGNAL(GUI._fromUtf8("clicked()")), self.generiereBuchstabe) QtCore.QObject.connect(self.ui.ResetLetter, QtCore.SIGNAL(GUI._fromUtf8("clicked()")), self.resetBuchstabe) QtCore.QObject.connect(self.ui.ResetLetter, QtCore.SIGNAL(GUI._fromUtf8("finished()")), self.resetBuchstabe) QtCore.QMetaObject.connectSlotsByName(self) self.setWindowTitle(PROGRAM_TITLE) self.showText(CREATING_WEIGHT_VECTOR) self.ui.fileReminder.setChecked(True) self.ui.listWidget.clear() self.ui.Depth.setMinimum(self.ui.minPartLength.value()) self.learner = LearningModule(EPSILON, LIMIT) self.showText(FIRST_THING_TO_DO) self.worker = WorkThread() def generiereBuchstabe(self): self.Wort = self.Wort + self.choiceMaker() if len(self.Wort) == 1: self.Wort = self.Wort.capitalize() self.showText(self.Wort) def choiceMaker(self): chances = {} chanceList = [] for char in self.learner.letters: chances[char] = [0, char] if len(self.Wort) < 1: return random.choice( string.ascii_uppercase ) #todo : Nur buchstaben verwenden, die in der Quelle vorkommen for wordPart in self.getWordParts(): try: for char in self.learner.weightVector[wordPart]: chances[char][0] = chances[char][ 0] + self.learner.weightVector[wordPart][char] except KeyError: pass #randomisieren for char in chances: chanceList.append( [chances[char][0] * random.random(), chances[char][1]]) self.printChanceList(chanceList) if max(chanceList)[0] == 0: return random.choice(['a', 'e', 'i', 'o', 'u']) else: return max( chanceList )[1] # waehle das groeßte element und nimm dazugehoerigen buchstaben def printChanceList(self, chanceList): for Chance in sorted(chanceList)[::-1]: print("%1s:%-4.f" % (Chance[1], Chance[0]), " ") print('\n') def getWordParts(self): wordParts = [] for i in range(1, DEPTH + 1): try: wordParts.append(self.Wort[-i:].lower()) except IndexError: pass return wordParts def resetBuchstabe(self): self.Wort = '' self.showText(self.Wort) def showText(self, text): self.ui.GeneratedWord.setText(str(text)) self.update() def generateWeightVector(self, path): if self.ui.fileReminder.checkState(): self.ui.listWidget.addItem(path) else: self.ui.listWidget.clear() self.ui.listWidget.addItem(path) self.Wort = '' self.learner.createWeightVector(path, self.ui.fileReminder.checkState(), self.ui.Depth.value(), self.ui.minPartLength.value(), self.ui.epsilon.value()) #self.learner = self.worker.run(self.learner,path,self.ui.fileReminder.checkState(),self.ui.Depth.value(),self.ui.minPartLength.value(), self.ui.epsilon.value()) if self.ui.saveWeightVector.isChecked(): self.learner.saveWeightVector() self.showText(YOU_CAN_START) def dragEnterEvent(self, event): if event.mimeData().hasText: event.accept() else: event.ignore() def dragMoveEvent(self, event): if event.mimeData().hasText: event.setDropAction(QtCore.Qt.CopyAction) event.accept() else: event.ignore() def dropEvent(self, event): if event.mimeData().hasText: self.showText(CREATING_WEIGHT_VECTOR) for url in event.mimeData().urls(): path = url.toLocalFile() self.generateWeightVector(path) else: event.ignore()
class MainWindows(QWidget): def __init__(self): super().__init__() self.gui = Ui_Form() self.gui.setupUi(self) self.player = QMediaPlayer() self.gui.voiceSlider.setMinimum(0) # 最小值 self.gui.voiceSlider.setMaximum(100) # 最大值 self.gui.voiceSlider.setSingleStep(1) # 步长 self.gui.voiceSlider.setValue(100) # 音量初始化 # 事件绑定 self.gui.search_button.clicked.connect(self.thread_search) # 搜索 self.gui.play_button.clicked.connect(self.thread_playmusic) # 播放 self.gui.list.doubleClicked.connect(self.thread_open_playlist) # 打开歌单 self.player.durationChanged.connect(self.set_time) # 初始化进度条 self.player.positionChanged.connect(self.play_slide) # 更新进度条 self.gui.voiceSlider.valueChanged.connect(self.changed_voice) def changed_voice(self): try: self.player.setVolume(self.gui.voiceSlider.value()) except: pass # 更新进度条 def play_slide(self): val = self.player.position() / 1000 self.gui.Slider.setValue(val) music_time = self.player.duration() / 1000 # 把毫秒转换为秒 # 现在时间 now_min = int(val // 60) # 分钟 now_s = int(val - now_min * 60) # 秒 # 总时间 all_min = int(int(music_time) // 60) # 分钟 all_s = int(music_time - all_min * 60) # 秒 self.gui.time_label.setText('{0}:{1}/{2}:{3}'.format( now_min, now_s, all_min, all_s)) # 初始化进度条 def set_time(self): self.gui.Slider.setValue(0) self.music_time = self.player.duration() / 1000 self.gui.Slider.setRange(0, int(self.music_time)) # 搜索 def search(self): search_input = self.gui.search_input.text() # 获取输入 search_type = self.gui.search_type.currentText() search = Search(type=search_type, keyword=search_input) self.search_return = search.run() self.gui.list.clear() if search_type == '单曲': for i in self.search_return: name = '{name}-{singer}'.format(name=i['name'], singer=i['singer']) self.gui.list.addItem(name) elif search_type == '歌单': for i in self.search_return: name = '{name}'.format(name=i['name']) self.gui.list.addItem(name) # 搜索线程 def thread_search(self): thread = Thread(target=self.search) thread.start() # 播放音乐 def playmusic(self): music_name = self.gui.list.currentItem().text() try: name = re.search(r'(^.*?)(-)(.*?)$', music_name).group(0) except AttributeError: pass if music_name != None: if self.gui.play_button.text() == '播放': if self.gui.search_type.currentText() == '单曲': for i in self.search_return: if i['name'] == music_name.split('-')[0]: id = i['id'] break elif self.gui.search_type.currentText() == '歌单': for i in self.musics_by_playlist: if i['name'] == music_name.split('-')[0]: id = i['id'] break music = Music(id=str(id)) if music_name != self.gui.label.text()[5:]: self.gui.label.setText('正在播放:' + music_name) music.download() path = r'cache\{0}.mp3'.format(name) url = QUrl.fromLocalFile(path) content = QMediaContent(url) self.player.setMedia(content) self.player.play() self.gui.play_button.setText('暂停') else: self.player.play() self.gui.play_button.setText('暂停') else: self.player.pause() self.gui.play_button.setText('播放') # 播放音乐线程 def thread_playmusic(self): thread = Thread(target=self.playmusic) thread.start() if self.gui.label.text() == '暂时还没有歌曲': self.gui.label.setText('正在加载,请稍等...') # 打开歌单 def open_playlist(self): if self.gui.search_type.currentText() == '歌单': playlist = self.gui.list.currentItem().text() for i in self.search_return: if i['name'] == playlist: id = i['id'] break self.musics_by_playlist, ids = find_id_in_playlist(id=id) self.gui.list.clear() for i in self.musics_by_playlist: name = '{name}-{singer}'.format(name=i['name'], singer=i['singer']) self.gui.list.addItem(name) for i in ids: music = Music(id=i) self.musics_by_playlist.append(music.detail) name = '{name}-{singer}'.format(name=music.name, singer=music.singer) self.gui.list.addItem(name) # 打开歌单进程 def thread_open_playlist(self): thread = Thread(target=self.open_playlist) thread.start()
if len(sys.argv) > 1: print_log = print print_log("Starting console version. \ For GUI start program without arguments.\n") update_progress = phldr args = parser.parse_args() unpack(args) else: app = QtWidgets.QApplication(sys.argv) #w = uic.loadUi("GUI.ui") window = QtWidgets.QWidget() w = Ui_Form() w.setupUi(window) unpack_thread = Thread() print_log = unpack_thread.print_c update_progress = unpack_thread.add_progress unpack_thread.print_call.connect(w.log_window.append) unpack_thread.progress_call.connect(w.progress.setValue) unpack_thread.complete_call.connect(complete) print_log("Starting GUI version. \ For console version use some arguments (like --help).\n") w.start.clicked.connect(start_unpack) w.in_place.clicked.connect(lock_dst)