def init_sounds(): for sound_name, value in sounds_list.items(): config = DEFAULT_CONFIG.copy() if isinstance(value, dict): config.update(value) else: config['file_name'] = value volume = float(config['volume']) volumes[sound_name] = volume vec = [] for i in range(CHANNELS_PER_FILE): s = QSoundEffect(app) s.statusChanged.connect( lambda i=i, s=s: log('stateChanged: {}: {}: {}'.format(s.source().toLocalFile(), i, s.status())) ) s.setLoopCount(int(config['loop_count'])) s.setVolume(volume) s.setSource(QUrl.fromLocalFile(config['file_name'])) vec.append(s) sounds[sound_name] = tuple(vec) #log('xxx: ' + str(sounds)) # start polling for messages def proc(): #while not queue.empty(): # queue.get(False) timer.start(10) QTimer.singleShot(500, proc)
def setupSound(self): print(os.getcwd()) good = QSoundEffect(self) good.setSource(QUrl.fromLocalFile(os.path.join("sound", "good.wav"))) good.setLoopCount(1) bad = QSoundEffect(self) bad.setSource(QUrl.fromLocalFile(os.path.join("sound", "bad.wav"))) bad.setLoopCount(1) self.sounds = {'good': good, 'bad': bad}
class Models: def __init__(self, base): self.base = base #======================== Images ======================= images = [] for file_ in os.listdir("./data"): x = 4 if file_.endswith((".jpg", ".jpeg", ".png")): images.append(file_) #======================== Sounds ======================= def initSound(self): self.select_sound = QSoundEffect(self.base) self.next_set_sound = QSoundEffect(self.base) self.expand_sound = QSoundEffect(self.base) self.shrink_sound = QSoundEffect(self.base) self.select_sound.setSource( QUrl.fromLocalFile(os.path.join('./data/sounds', 'click.wav'))) self.shrink_sound.setSource( QUrl.fromLocalFile(os.path.join('./data/sounds', 'click.wav'))) self.next_set_sound.setSource( QUrl.fromLocalFile(os.path.join('./data/sounds', 'camera-roll.wav'))) self.expand_sound.setSource( QUrl.fromLocalFile(os.path.join('./data/sounds', 'expand.wav'))) self.select_sound.setLoopCount(0) self.shrink_sound.setLoopCount(0) self.expand_sound.setLoopCount(0) self.next_set_sound.setLoopCount(0)
class noiseGenerator(QThread): def __init__(self, playnoise, volume): super().__init__() pathname = os.path.dirname(os.path.realpath('whitenoise.wav')) self.sound = QSoundEffect() self.sound.setSource(QUrl.fromLocalFile(pathname + '/whitenoise.wav')) self.sound.setLoopCount(QSoundEffect.Infinite) self.settings(playnoise, volume) def settings(self, playnoise, volume): self.playnoise = playnoise self.sound.setVolume(volume) def run(self): if self.playnoise: self.sound.play() def stop(self): self.sound.stop()
class Sound(object): def __init__(self, filename, volume): super(Sound, self).__init__() file = os.path.join(BASEDIR, 'sounds', filename) self.volume = volume or 0 self.effect = QSoundEffect() self.effect.setSource(QUrl.fromLocalFile(file)) def play(self, volume=None, loop=True): volume = self.volume if volume is None else volume self.effect.setLoopCount(0) self.effect.setVolume(int(volume) / 100) if loop: self.effect.setLoopCount(QSoundEffect.Infinite) else: self.effect.setLoopCount(1) if not self.effect.isPlaying(): self.effect.play() def stop(self): self.effect.stop() def setVolume(self, volume): self.volume = volume
class Window(QWidget): def __init__(self, width, height): super().__init__() self.height = height self.width = width self.index = 0 self.leftBreak = 0 self.rightBreak = 4 self.bigPixList = [] self.pixList = [] self.label = [] self.tagLabels = [] self.bigLabel = QLabel(self) self.bigLabel.resize(self.width * 3 / 4, self.height * 3 / 4) self.bigLabel.move(self.width / 8, self.height / 8) self.bigLabel.hide() #adding buttons and textbox self.textBox = QLineEdit(self) self.textBox.setStyleSheet("color: rgb(255, 255, 255);") self.textBox.move(self.width / 6, self.height * 7 / 8) self.textBox.resize(150, self.height / 16) self.textBox.hide() self.tagButton = QPushButton('Add Tag', self) self.tagButton.setStyleSheet("background-color: rgb(125,125,125);") self.tagButton.move(self.width / 6, self.height * 15 / 16) self.tagButton.clicked.connect(self.tagClick) self.tagButton.hide() self.saveTagButton = QPushButton('Save All Tags', self) self.saveTagButton.setStyleSheet( "background-color: rgb(125, 125, 125);") self.saveTagButton.move(self.width / 6 + 75, self.height * 15 / 16) self.saveTagButton.clicked.connect(self.saveClick) self.saveTagButton.hide() self.setFocus() #item that holds the current string to be stored as tag self.currentString = [] self.mode = 0 #Sets up the warning message for too long strings self.warningMessage = QLabel(self) self.warningMessage.resize(self.width / 5, self.height / 16) self.warningMessage.setStyleSheet( "background-color: red; font: bold 14px") self.warningMessage.setText("String too long") self.warningMessage.move(self.width / 2, self.height * 7 / 8) self.warningMessage.hide() #setting up the sounds to use self.soundClick = QSoundEffect() self.soundClickWah = QSoundEffect() self.soundLoop = QSoundEffect() self.soundLoopWah = QSoundEffect() self.soundShift = QSoundEffect() self.soundShiftWah = QSoundEffect() self.soundClick.setSource( QUrl.fromLocalFile(os.path.join('sounds', 'PianoNote.wav'))) self.soundClickWah.setSource( QUrl.fromLocalFile(os.path.join('sounds', 'PianoNoteWah.wav'))) self.soundShift.setSource( QUrl.fromLocalFile(os.path.join('sounds', 'PianoMultiNote.wav'))) self.soundShiftWah.setSource( QUrl.fromLocalFile(os.path.join('sounds', 'PianoMultiNoteWah.wav'))) self.soundLoop.setSource( QUrl.fromLocalFile(os.path.join('sounds', 'loop08.wav'))) self.soundLoopWah.setSource( QUrl.fromLocalFile(os.path.join('sounds', 'loop08Wah.wav'))) self.soundLoop.setLoopCount(QSoundEffect.Infinite) self.soundLoopWah.setLoopCount(QSoundEffect.Infinite) self.soundLoop.play() self.soundLoopWah.play() self.soundLoopWah.setMuted(1) self.initUI() #Adds the tag to the list triggered by clicking on the button only if the string is less than 11 characters, otherwise shows warning message def tagClick(self): tagValue = self.textBox.text() if (len(tagValue) < 11): self.currentString[self.index % len(self.pixList)] += (tagValue + "\n") self.tagLabels[self.index % len(self.pixList)].setText( self.currentString[self.index % len(self.pixList)]) self.textBox.setText("") self.setFocus() self.warningMessage.hide() else: self.warningMessage.show() def saveClick(self): f = open('SavedTags.txt', 'w') f.truncate() for i in range(0, len(self.pixList), 1): f.write(self.tagLabels[i].text()) f.write("#") def initUI(self): # title of window self.setWindowTitle('PyQt5 Main Window') # place window on screen at x=0, y=0 self.setGeometry(0, 0, self.width, self.height) self.setStyleSheet('background-color: black') #sets up QLabels for later input for i in range(0, 5, 1): self.label.append(QLabel(self)) self.label[i].move(self.width / 12 + i * self.width / 6, self.height / 3) self.label[i].resize(self.width / 6, self.height / 6) self.label[i].setStyleSheet('background-color: red') if (i == 0): self.label[i].setStyleSheet('background-color: blue') #places pictures into pixmap array i = 0 for file in os.listdir('data'): self.pixList.append(QPixmap(os.path.join('data', file))) self.bigPixList.append(QPixmap(os.path.join('data', file))) self.tagLabels.append(QLabel(self)) self.currentString.append("") self.tagLabels[i].resize(self.width / 8, self.height) self.tagLabels[i].move(self.width * 7 / 8, 0) self.tagLabels[i].setStyleSheet( 'border-color: grey; border-style: outset; border-width: 5px; font: bold 14px; color: white' ) self.tagLabels[i].setAlignment(Qt.AlignTop) self.tagLabels[i].hide() if (self.pixList[i].height() > self.height / 6 - 10): self.pixList[i] = self.pixList[i].scaledToHeight(self.height / 6 - 10) if (self.pixList[i].width() > self.width / 6 - 10): self.pixList[i] = self.pixList[i].scaledToWidth(self.width / 6 - 10) if (self.bigPixList[i].width() > self.width * 3 / 4): self.bigPixList[i] = self.bigPixList[i].scaledToWidth( self.width * 3 / 4) if (self.bigPixList[i].height() > self.height * 3 / 4): self.bigPixList[i] = self.bigPixList[i].scaledToHeight( self.height * 3 / 4) i = i + 1 self.bigLabel.setAlignment(Qt.AlignCenter) self.bigLabel.setPixmap(self.bigPixList[0]) #puts initial pixmaps into the designated qlabels for i in range(0, 5, 1): self.label[i].setPixmap(self.pixList[i]) self.label[i].setAlignment(Qt.AlignCenter) self.show() self.loadTags() def loadTags(self): f = open('SavedTags.txt', 'r') tempString = f.read() j = 0 for i in range(0, len(tempString), 1): if (tempString[i] != "#"): self.currentString[j] += tempString[i] else: self.tagLabels[j].setText(self.currentString[j]) j = j + 1 #Moves the pointer to the picture one to the left. If it breaks the bounds, it will move the frame def moveIndexLeft(self): j = 0 if (self.mode == 1): self.tagLabels[self.index % len(self.pixList)].hide() self.tagLabels[(self.index - 1) % len(self.pixList)].show() self.label[self.index % 5].setStyleSheet('background-color:red') self.index = self.index - 1 if (self.index < self.leftBreak): self.leftBreak = self.leftBreak - 5 self.rightBreak = self.rightBreak - 5 for i in range(self.leftBreak, 1 + self.rightBreak, 1): self.label[j].setPixmap(self.pixList[i % len(self.pixList)]) self.label[j].setAlignment(Qt.AlignCenter) j = j + 1 self.label[self.index % 5].setStyleSheet('background-color: blue') self.bigLabel.setPixmap(self.bigPixList[self.index % len(self.pixList)]) self.textBox.setText("") #Moves the pointer one picture to the right. If it breaks the bounds of QLabel it will move the frame def moveIndexRight(self): j = 0 if (self.mode == 1): self.tagLabels[self.index % len(self.pixList)].hide() self.tagLabels[(self.index + 1) % len(self.pixList)].show() self.label[self.index % 5].setStyleSheet('background-color: red') self.index = self.index + 1 if (self.index > self.rightBreak): self.leftBreak = self.leftBreak + 5 self.rightBreak = self.rightBreak + 5 for i in range(self.leftBreak, 1 + self.rightBreak, 1): self.label[j].setPixmap(self.pixList[i % len(self.pixList)]) self.label[j].setAlignment(Qt.AlignCenter) j = j + 1 self.label[self.index % 5].setStyleSheet('background-color: blue') self.bigLabel.setPixmap(self.bigPixList[self.index % len(self.pixList)]) self.textBox.setText("") #Zooms in on the specific picture selected and puts it into a 700 x 500 frame def zoomIn(self): self.mode = 1 for i in range(0, 5, 1): self.label[i].hide() self.bigLabel.setAlignment(Qt.AlignCenter) self.bigLabel.show() self.tagButton.show() self.saveTagButton.show() self.textBox.show() self.tagLabels[self.index % len(self.pixList)].show() #Goes back to default view def zoomOut(self): self.mode = 0 self.bigLabel.hide() for i in range(0, 5, 1): self.label[i].show() self.tagButton.hide() self.saveTagButton.hide() self.textBox.hide() self.tagLabels[self.index % len(self.pixList)].hide() #shifts the frame 5 pictures to the left def shiftLeft(self): if (self.mode == 1): self.tagLabels[self.index % len(self.pixList)].hide() self.label[self.index % 5].setStyleSheet('background-color:red') j = 0 self.index = self.leftBreak - 1 if (self.mode == 1): self.tagLabels[self.index % len(self.pixList)].show() self.leftBreak = self.leftBreak - 5 self.rightBreak = self.rightBreak - 5 for i in range(self.leftBreak, 1 + self.rightBreak, 1): self.label[j].setPixmap(self.pixList[i % len(self.pixList)]) j = j + 1 self.label[self.index % 5].setStyleSheet('background-color: blue') self.bigLabel.setPixmap(self.bigPixList[self.index % len(self.pixList)]) self.textBox.setText("") #shifts the frame 5 pictures to the right def shiftRight(self): if (self.mode == 1): self.tagLabels[self.index % len(self.pixList)].hide() self.label[self.index % 5].setStyleSheet('background-color: red') j = 0 self.index = self.rightBreak + 1 if (self.mode == 1): self.tagLabels[self.index % len(self.pixList)].show() self.rightBreak = self.rightBreak + 5 self.leftBreak = self.leftBreak + 5 for i in range(self.leftBreak, 1 + self.rightBreak, 1): self.label[j].setPixmap(self.pixList[i % len(self.pixList)]) j = j + 1 self.label[self.index % 5].setStyleSheet('background-color: blue') self.bigLabel.setPixmap(self.bigPixList[self.index % len(self.pixList)]) self.textBox.setText("") #all of the key inputs and their responses in functions def keyPressEvent(self, event): if (event.key() == 16777234): self.moveIndexLeft() if (self.mode == 0): self.soundClick.play() else: self.soundClickWah.play() if (event.key() == 16777236): self.moveIndexRight() if (self.mode == 0): self.soundClick.play() else: self.soundClickWah.play() if (event.key() == 16777235): self.zoomIn() self.soundLoop.setMuted(1) self.soundLoopWah.setMuted(0) if (event.key() == 16777237): self.zoomOut() self.soundLoopWah.setMuted(1) self.soundLoop.setMuted(0) if (event.key() == 44): self.shiftLeft() if (self.mode == 0): self.soundShift.play() else: self.soundShiftWah.play() if (event.key() == 46): self.shiftRight() if (self.mode == 0): self.soundShift.play() else: self.soundShiftWah.play() def mousePressEvent(self, QMouseEvent): if (QMouseEvent.y() < self.height / 7 * 8 or QMouseEvent.y() > self.height / 15 * 16 and QMouseEvent.x() < self.width / 6 or QMouseEvent.x() > self.width / 3): self.setFocus() if (self.mode == 0): setPicTo = -1 if (QMouseEvent.y() > self.height / 3 - 1 and QMouseEvent.y() < self.height / 2 + 1): if (QMouseEvent.x() > self.width / 12 and QMouseEvent.x() < self.width * 3 / 12 + 1): setPicTo = 0 if (QMouseEvent.x() > self.width * 3 / 12 and QMouseEvent.x() < self.width * 5 / 12 + 1): setPicTo = 1 if (QMouseEvent.x() > self.width * 5 / 12 and QMouseEvent.x() < self.width * 7 / 12 + 1): setPicTo = 2 if (QMouseEvent.x() > self.width * 7 / 12 and QMouseEvent.x() < self.width * 9 / 12 + 1): setPicTo = 3 if (QMouseEvent.x() > self.width * 9 / 12 and QMouseEvent.x() < self.width * 11 / 12 + 1): setPicTo = 4 if (setPicTo > -1): self.label[self.index % 5].setStyleSheet('background-color:red') self.mode = 1 self.index = self.leftBreak + setPicTo self.label[self.index % 5].setStyleSheet('background-color:blue') self.bigLabel.setPixmap(self.bigPixList[self.index % len(self.pixList)]) for i in range(0, 5, 1): self.label[i].hide() self.bigLabel.setAlignment(Qt.AlignCenter) self.bigLabel.show()
def on_btnEffect_Resource_clicked(self): url = QUrl.fromLocalFile(":/Wave/sound/blast.wav") player = QSoundEffect(self) player.setLoopCount(2) player.setSource(url) player.play() #无法播放资源文件
def on_btnEffect_File_clicked(self): url = QUrl.fromLocalFile("Ak47.wav") player = QSoundEffect(self) player.setLoopCount(2) #播放循环次数 player.setSource(url) #设置源文件 player.play()
class Window(QWidget): def __init__(self,width,height): super().__init__() self.height = height self.width = width #index for the navigation self.index = 0 #index for changing the color of the selected image self.colorIndex = 0 #determines when to load new images self.leftBreak = 0 self.rightBreak = 4 #large zoomed in image list self.bigPixList = [] #thumbnail image list self.pixList = [] #labels to store thumbnails self.label = [] #labels to store tags self.tagLabels = [] #big label for zoomed in image self.bigLabel = QLabel(self) self.bigLabel.resize(self.width * 3 / 4, self.height * 3 / 4) self.bigLabel.move(self.width / 8, self.height / 8) self.bigLabel.hide() #for saving all of the search images self.saveURL = "" #adding buttons and textbox self.textBox = QLineEdit(self) self.textBox.setStyleSheet("color: rgb(255, 255, 255);") self.textBox.move(self.width / 6, self.height * 7 / 8) self.textBox.resize(150,self.height / 16) self.textBox.hide() self.tagButton = QPushButton('Add Tag', self) self.tagButton.setStyleSheet("background-color: rgb(125,125,125);") self.tagButton.move(self.width / 6, self.height * 15 / 16) self.tagButton.clicked.connect(self.tagClick) self.tagButton.hide() self.saveTagButton = QPushButton('Save All Tags', self) self.saveTagButton.setStyleSheet("background-color: rgb(125, 125, 125);") self.saveTagButton.move(self.width / 6 + 75, self.height * 15 / 16) self.saveTagButton.clicked.connect(self.saveTagClick) self.saveTagButton.hide() self.searchBar = QLineEdit(self) self.searchBar.setStyleSheet("color: rgb(255, 255,255);") self.searchBar.move(self.width/6, self.height * 7 / 8) self.searchBar.resize(300, self.height / 16) self.setFocus() self.testButton = QPushButton('Test', self) self.testButton.setStyleSheet("background-color: rgb(125, 125, 125);") self.testButton.move(self.width / 6, self.height * 15 / 16) self.testButton.clicked.connect(self.testClick) self.saveButton = QPushButton('Save', self) self.saveButton.setStyleSheet("background-color: rgb(125, 125, 125);") self.saveButton.move(self.width / 6 + 75, self.height * 15 / 16) self.saveButton.clicked.connect(self.saveClick) self.exitButton = QPushButton('Exit',self) self.exitButton.setStyleSheet("background-color: rgb(125, 125, 125);") self.exitButton.move(self.width / 6 + 150, self.height * 15 / 16) self.exitButton.clicked.connect(self.exitClick) self.deleteButton = QPushButton('Delete', self) self.deleteButton.setStyleSheet("background-color: rgb(125, 125, 125);") self.deleteButton.move(self.width / 6 + 225, self.height * 15 / 16) self.deleteButton.clicked.connect(self.deleteClick) self.searchButton = QPushButton('Search', self) self.searchButton.setStyleSheet("background-color: rgb(125, 125, 125);") self.searchButton.clicked.connect(self.searchClick) self.searchButton.move(self.width/6 +325, self.height * 29 / 32) self.searchResults = QLineEdit(self) self.searchResults.setStyleSheet("background-color: rgb(255, 255, 255);") self.searchResults.resize(25, self. height / 16) self.searchResults.move(self.width/6 + 425, self.height * 29 / 32) self.searchResults.setText("10") self.searchText = QLabel(self) self.searchText.resize(150, 50) self.searchText.move(self.width/6 + 455, self.height * 29 / 32) self.searchText.setText("Max Search Results") self.searchText.setStyleSheet("color: rgb(255, 255, 255);") #item that holds the current string to be stored as tag self.currentString = [] self.mode = 0 #Sets up the warning message for too long strings self.warningMessage = QLabel(self) self.warningMessage.resize(self.width / 5, self.height / 16) self.warningMessage.setStyleSheet("background-color: red; font: bold 14px") self.warningMessage.setText("String too long") self.warningMessage.move(self.width / 2, self.height * 7 / 8) self.warningMessage.hide() #setting up the sounds to use self.soundClick = QSoundEffect() self.soundClickWah = QSoundEffect() self.soundLoop = QSoundEffect() self.soundLoopWah = QSoundEffect() self.soundShift = QSoundEffect() self.soundShiftWah = QSoundEffect() self.soundClick.setSource(QUrl.fromLocalFile(os.path.join('sounds','PianoNote.wav'))) self.soundClickWah.setSource(QUrl.fromLocalFile(os.path.join('sounds','PianoNoteWah.wav'))) self.soundShift.setSource(QUrl.fromLocalFile(os.path.join('sounds','PianoMultiNote.wav'))) self.soundShiftWah.setSource(QUrl.fromLocalFile(os.path.join('sounds','PianoMultiNoteWah.wav'))) self.soundLoop.setSource(QUrl.fromLocalFile(os.path.join('sounds','loop08.wav'))) self.soundLoopWah.setSource(QUrl.fromLocalFile(os.path.join('sounds','loop08Wah.wav'))) self.soundLoop.setLoopCount(QSoundEffect.Infinite) self.soundLoopWah.setLoopCount(QSoundEffect.Infinite) self.soundLoop.play() self.soundLoopWah.play() self.soundLoopWah.setMuted(1) self.initUI() #Adds the tag to the list triggered by clicking on the button only if the string is less than 11 characters, otherwise shows warning message def tagClick(self): tagValue = self.textBox.text() if(len(tagValue) < 11): self.currentString[self.index % len(self.pixList)] += (tagValue + "\n") self.tagLabels[self.index % len(self.pixList)].setText(self.currentString[self.index % len(self.pixList)]) self.textBox.setText("") self.setFocus() self.warningMessage.hide() else: self.warningMessage.show() def saveTagClick(self): f = open('SavedTags.txt','w') f.truncate() for i in range(0, len(self.pixList), 1): f.write(self.tagLabels[i].text()) f.write("#") self.setFocus() #takes a URL entered into the search bar and adds an image to the two pixmap lists. It also appends the URL onto the saved URLs string if the user wants to save them later def testClick(self): path = self.searchBar.text() self.searchBar.setText("") self.saveURL = self.saveURL + path + "\n" url_data = urllib.request.urlopen(path).read() tmpPixmap = QPixmap() tmpPixmap.loadFromData(url_data) if(tmpPixmap.height() > self.height * 3 / 4): tmpPixmap = tmpPixmap.scaledToHeight(self.height * 3 / 4) if(tmpPixmap.width() > self.width * 3 / 4): tmpPixmap = tmpPixmap.scaledToWidth(self.width * 3 / 4) self.bigPixList.append(tmpPixmap) if(tmpPixmap.height() > self.height / 6 - 10): tmpPixmap = tmpPixmap.scaledToHeight(self.height / 6 - 10) if(tmpPixmap.width() > self.width / 6 - 10): tmpPixmap = tmpPixmap.scaledToWidth(self.width / 6 - 10) self.pixList.append(tmpPixmap) for i in range(-1,4,1): self.label[i + 1].setPixmap(self.pixList[i % len(self.pixList)]) self.label[self.colorIndex].setStyleSheet('background-color: red') self.index = -1 self.colorIndex = 0 self.leftBreak = -1 self.rightBreak = 3 self.label[self.colorIndex].setStyleSheet('background-color: blue') self.tagLabels.append(self.tagLabels[i]) self.tagLabels[-1 % len(self.pixList)].setText("") self.bigLabel.setPixmap(self.bigPixList[-1 % len(self.pixList)]) self.setFocus() #saves all the URLS searched for into a text file separated by a new line def saveClick(self): f = open('SavedURLS.txt','w') f.truncate() f.write(self.saveURL) self.saveTags() self.setFocus() #exits the program def exitClick(self): sys.exit() #simply deletes the image out of the array and fixes it so that the images get truncated to fit def deleteClick(self): del self.pixList[self.index % len(self.pixList)] del self.bigPixList[self.index % len(self.pixList)] self.bigLabel.setPixmap(self.bigPixList[self.index % len(self.pixList)]) for i in range(0, 5, 1): self.label[i].setPixmap(self.pixList[i + self.leftBreak]) self.setFocus() #searches using the flickr API and json responses. Stores those images in the same two pixmap lists and appends the URLS to the saved urls string if the user wants to save them def searchClick(self): self.index = len(self.pixList) self.leftBreak = self.index self.rightBreak = self.index + 4 self.label[self.colorIndex].setStyleSheet('background-color: red') self.colorIndex = 0 self.label[0].setStyleSheet('background-color: blue') tempStr = self.searchBar.text() tempStr.replace(" ", "%20") self.searchBar.setText("") req = 'https://api.flickr.com/services/rest/' req = req + '?method=flickr.photos.search' req = req + '&per_page=' + self.searchResults.text() req = req + '&format=json&nojsoncallback=1' req = req + '&api_key=a0ab0abe9800e8352ae0364a9b595cd2' req = req + '&tags=' + tempStr jsonRespDict = requests.get(req).json() photoset = jsonRespDict['photos'] for p in photoset['photo']: farm = p['farm'] server = p['server'] eyedee = p['id'] secret = p['secret'] tempStr = "http://farm" +str(farm) + ".static.flickr.com/" + str(server) + "/" + str(eyedee) + "_" + str(secret) + ".jpg" print(tempStr) self.saveURL = self.saveURL + tempStr + "\n" url_data = urllib.request.urlopen(tempStr).read() tmpPixmap = QPixmap() tmpPixmap.loadFromData(url_data) if(tmpPixmap.height() > self.height * 3 / 4): tmpPixmap = tmpPixmap.scaledToHeight(self.height * 3 / 4) if(tmpPixmap.width() > self.width * 3 / 4): tmpPixmap = tmpPixmap.scaledToWidth(self.width * 3 / 4) self.bigPixList.append(tmpPixmap) if(tmpPixmap.height() > self.height / 6 - 10): tmpPixmap = tmpPixmap.scaledToHeight(self.height / 6 - 10) if(tmpPixmap.width() > self.width / 6 - 10): tmpPixmap = tmpPixmap.scaledToWidth(self.width / 6 - 10) self.pixList.append(tmpPixmap) self.tagLabels.append(self.tagLabels[0]) for i in range(self.index, len(self.pixList), 1): self.tagLabels[i].setText("") for i in range(self.index, self.index + 5, 1): self.label[i-self.index].setPixmap(self.pixList[i % len(self.pixList)]) self.bigLabel.setPixmap(self.bigPixList[self.index % len(self.pixList)]) self.setFocus() def initUI(self): # title of window self.setWindowTitle('PyQt5 Main Window') # place window on screen at x=0, y=0 self.setGeometry(0, 0, self.width, self.height) self.setStyleSheet('background-color: black') #sets up QLabels for later input for i in range(0, 5, 1): self.label.append(QLabel(self)) self.label[i].move(self.width / 12 + i * self.width / 6, self.height / 3) self.label[i].resize(self.width / 6, self.height / 6) self.label[i].setStyleSheet('background-color: red') if(i == 0): self.label[i].setStyleSheet('background-color: blue') #places pictures into pixmap array i = 0 for file in os.listdir('data'): self.pixList.append(QPixmap(os.path.join('data', file))) self.bigPixList.append(QPixmap(os.path.join('data',file))) self.tagLabels.append(QLabel(self)) self.currentString.append("") self.tagLabels[i].resize(self.width / 8, self.height) self.tagLabels[i].move(self.width * 7 / 8, 0) self.tagLabels[i].setStyleSheet('border-color: grey; border-style: outset; border-width: 5px; font: bold 14px; color: white') self.tagLabels[i].setAlignment(Qt.AlignTop) self.tagLabels[i].hide() if(self.pixList[i].height() > self.height / 6 - 10): self.pixList[i] = self.pixList[i].scaledToHeight(self.height / 6 - 10) if(self.pixList[i].width() > self.width / 6 - 10): self.pixList[i] = self.pixList[i].scaledToWidth(self.width / 6 - 10) if(self.bigPixList[i].width() > self.width*3/4 ): self.bigPixList[i] = self.bigPixList[i].scaledToWidth(self.width*3/4) if(self.bigPixList[i].height() > self.height*3/4): self.bigPixList[i] = self.bigPixList[i].scaledToHeight(self.height*3/4) i = i + 1 self.bigLabel.setAlignment(Qt.AlignCenter) self.bigLabel.setPixmap(self.bigPixList[0]) #puts initial pixmaps into the designated qlabels for i in range(0, 5, 1): self.label[i].setPixmap(self.pixList[i]) self.label[i].setAlignment(Qt.AlignCenter) self.loadURLS() self.show() self.loadTags() #Loads URLS from the text file and places them into the pixmap array def loadURLS(self): f = open('SavedURLS.txt','r') tempString = f.read() self.savedURLS = tempString tempString2 = tempString.split("\n") for i in range(0, len(tempString2)-1, 1): url_data = urllib.request.urlopen(tempString2[i]).read() tmpPixmap = QPixmap() tmpPixmap.loadFromData(url_data) if(tmpPixmap.height() > self.height * 3 / 4): tmpPixmap = tmpPixmap.scaledToHeight(self.height * 3 / 4) if(tmpPixmap.width() > self.width * 3 / 4): tmpPixmap = tmpPixmap.scaledToWidth(self.width * 3 / 4) self.bigPixList.append(tmpPixmap) if(tmpPixmap.height() > self.height / 6 - 10): tmpPixmap = tmpPixmap.scaledToHeight(self.height / 6 - 10) if(tmpPixmap.width() > self.width / 6 - 10): tmpPixmap = tmpPixmap.scaledToWidth(self.width / 6 - 10) self.pixList.append(tmpPixmap) self.tagLabels.append(QLabel(self)) self.currentString.append("") self.tagLabels[len(self.pixList)-1].resize(self.width / 8, self.height) self.tagLabels[len(self.pixList)-1].move(self.width * 7 / 8, 0) self.tagLabels[len(self.pixList)-1].setStyleSheet('border-color: grey; border-style: outset; border-width: 5px; font:bold 14px; color: white') self.tagLabels[len(self.pixList)-1].setAlignment(Qt.AlignTop) self.tagLabels[len(self.pixList)-1].hide() #loads all saved tags def loadTags(self): f = open('SavedTags.txt','r') tempString = f.read() j = 0 for i in range(0, len(tempString), 1): if(tempString[i] != "#"): self.currentString[j]+=tempString[i] else: self.tagLabels[j].setText(self.currentString[j]) j = j + 1 #Moves the pointer to the picture one to the left. If it breaks the bounds, it will move the frame def moveIndexLeft(self): j = 0 if(self.mode == 1): self.tagLabels[self.index % len(self.pixList)].hide() self.tagLabels[(self.index - 1) % len(self.pixList)].show() self.label[self.colorIndex].setStyleSheet('background-color:red') self.colorIndex = self.colorIndex -1 if(self.colorIndex < 0): self.colorIndex = 4 self.index = self.index - 1 if(self.index < self.leftBreak): self.leftBreak = self.leftBreak - 5 self.rightBreak = self.rightBreak - 5 for i in range(self.leftBreak, 1 + self.rightBreak, 1): self.label[j].setPixmap(self.pixList[i % len(self.pixList)]) self.label[j].setAlignment(Qt.AlignCenter) j = j + 1 self.label[self.colorIndex].setStyleSheet('background-color: blue') self.bigLabel.setPixmap(self.bigPixList[self.index % len(self.pixList)]) self.textBox.setText("") #Moves the pointer one picture to the right. If it breaks the bounds of QLabel it will move the frame def moveIndexRight(self): j = 0 if(self.mode == 1): self.tagLabels[self.index % len(self.pixList)].hide() self.tagLabels[(self.index + 1) % len(self.pixList)].show() self.label[self.colorIndex].setStyleSheet('background-color: red') self.colorIndex = self.colorIndex + 1 if(self.colorIndex > 4): self.colorIndex = 0 self.index = self.index + 1 if(self.index > self.rightBreak): self.leftBreak = self.leftBreak + 5 self.rightBreak = self.rightBreak + 5 for i in range(self.leftBreak, 1 + self.rightBreak, 1): self.label[j].setPixmap(self.pixList[i % len(self.pixList)]) self.label[j].setAlignment(Qt.AlignCenter) j = j + 1 self.label[self.colorIndex].setStyleSheet('background-color: blue') self.bigLabel.setPixmap(self.bigPixList[self.index % len(self.pixList)]) self.textBox.setText("") #Zooms in on the specific picture selected and puts it into a 700 x 500 frame def zoomIn(self): self.mode = 1 for i in range(0, 5, 1): self.label[i].hide() self.bigLabel.setAlignment(Qt.AlignCenter) self.bigLabel.show() self.tagButton.show() self.saveTagButton.show() self.textBox.show() self.tagLabels[self.index % len(self.pixList)].show() self.searchBar.hide() self.testButton.hide() self.saveButton.hide() self.exitButton.hide() self.deleteButton.hide() self.searchButton.hide() self.searchResults.hide() self.searchText.hide() #Goes back to default view def zoomOut(self): self.mode = 0 self.bigLabel.hide() for i in range(0, 5, 1): self.label[i].show() self.tagButton.hide() self.saveTagButton.hide() self.textBox.hide() self.tagLabels[self.index % len(self.pixList)].hide() self.searchBar.show() self.testButton.show() self.saveButton.show() self.exitButton.show() self.deleteButton.show() self.searchButton.show() self.searchResults.show() self.searchText.show() #shifts the frame 5 pictures to the left def shiftLeft(self): if(self.mode == 1): self.tagLabels[self.index % len(self.pixList)].hide() self.label[self.colorIndex].setStyleSheet('background-color:red') j = 0 self.colorIndex = 0 self.index = self.leftBreak - 5 if(self.mode == 1): self.tagLabels[self.index % len(self.pixList)].show() self.leftBreak = self.leftBreak - 5 self.rightBreak = self.rightBreak - 5 for i in range(self.leftBreak, 1 + self.rightBreak, 1): self.label[j].setPixmap(self.pixList[i % len(self.pixList)]) j = j + 1 self.label[0].setStyleSheet('background-color: blue') self.bigLabel.setPixmap(self.bigPixList[self.index % len(self.pixList)]) self.textBox.setText("") #shifts the frame 5 pictures to the right def shiftRight(self): if(self.mode == 1): self.tagLabels[self.index % len(self.pixList)].hide() self.label[self.colorIndex].setStyleSheet('background-color: red') self.colorIndex = 0 j = 0 self.index = self.rightBreak + 1 if(self.mode == 1): self.tagLabels[self.index % len(self.pixList)].show() self.rightBreak = self.rightBreak + 5 self.leftBreak = self.leftBreak + 5 for i in range(self.leftBreak, 1 + self.rightBreak, 1): self.label[j].setPixmap(self.pixList[i % len(self.pixList)]) j = j + 1 self.label[0].setStyleSheet('background-color: blue') self.bigLabel.setPixmap(self.bigPixList[self.index % len(self.pixList)]) self.textBox.setText("") #all of the key inputs and their responses in functions def keyPressEvent(self, event): if(event.key() == 16777234): self.moveIndexLeft() if(self.mode == 0): self.soundClick.play() else: self.soundClickWah.play() if(event.key() == 16777236): self.moveIndexRight() if(self.mode == 0): self.soundClick.play() else: self.soundClickWah.play() if(event.key() == 16777235): self.zoomIn() self.soundLoop.setMuted(1) self.soundLoopWah.setMuted(0) if(event.key() == 16777237): self.zoomOut() self.soundLoopWah.setMuted(1) self.soundLoop.setMuted(0) if(event.key() == 44): self.shiftLeft() if(self.mode == 0): self.soundShift.play() else: self.soundShiftWah.play() if(event.key() == 46): self.shiftRight() if(self.mode == 0): self.soundShift.play() else: self.soundShiftWah.play() def mousePressEvent(self, QMouseEvent): if(QMouseEvent.y() < self.height / 7 * 8 or QMouseEvent.y() > self.height / 15 * 16 and QMouseEvent.x() < self.width / 6 or QMouseEvent.x() > self.width / 3): self.setFocus() if(self.mode == 0): setPicTo = -1 if(QMouseEvent.y() > self.height / 3 - 1 and QMouseEvent.y() < self.height / 2 + 1): if(QMouseEvent.x() > self.width / 12 and QMouseEvent.x() < self.width * 3 / 12 + 1): setPicTo = 0 if(QMouseEvent.x() > self.width * 3 / 12 and QMouseEvent.x() < self.width * 5 / 12 + 1): setPicTo = 1 if(QMouseEvent.x() > self.width * 5 / 12 and QMouseEvent.x() < self.width * 7 / 12 + 1): setPicTo = 2 if(QMouseEvent.x() > self.width * 7 / 12 and QMouseEvent.x() < self.width * 9 / 12 + 1): setPicTo = 3 if(QMouseEvent.x() > self.width * 9 / 12 and QMouseEvent.x() < self.width * 11 / 12 + 1): setPicTo = 4 if(setPicTo > -1): self.label[self.index % 5].setStyleSheet('background-color:red') self.mode = 1 self.index = self.leftBreak + setPicTo self.colorIndex = setPicTo self.label[self.index % 5].setStyleSheet('background-color:blue') self.bigLabel.setPixmap(self.bigPixList[self.index % len(self.pixList)]) for i in range(0, 5, 1): self.label[i].hide() self.bigLabel.setAlignment(Qt.AlignCenter) self.bigLabel.show()
class QAVButton(QPushButton): def __init__(self, label): """ builds a custom button and displays it""" # calls super constuctor super(QAVButton, self).__init__(label) self.sound = QSoundEffect() self.volume = 1. self.color = QColor(Qt.gray) self.count = 0 self.duration = 1000 self.rate = 20 self.timer = QTimer() self.timer.timeout.connect(self.update_anim) self.mode = "sin" self.pressed.connect(self.start) self.released.connect(self.stop) self.is_accelerating = False self.update_color_with_alha(1) def setSound(self, sound_file): self.sound.setSource(QUrl.fromLocalFile(sound_file)) self.sound.setLoopCount(QSoundEffect.Infinite) def start(self): self.count = 0 self.sound.play() self.timer.start(self.rate) def stop(self): self.timer.stop() self.sound.stop() def set_color(self, col): self.color = col self.update_color_with_alha(1) def update_color_with_alha(self, alpha): red = self.color.red() green = self.color.green() blue = self.color.blue() bg_style = f"background-color:rgba({red},{green},{blue}, {alpha})" self.setStyleSheet(bg_style) def update_anim(self): #logarithmic (check with perception of luminosity/brighness) #val = math.log(count + 1) #linear if self.mode == "sin": val = math.sin(self.count * 2 * math.pi) / 2 + 0.5 elif self.mode == "lin": val = 1 - self.count else: val = math.log(self.count + 1) alpha = round(val * 100) + 155 self.update_color_with_alha(alpha) amplitude = val * 0.8 + 0.2 self.sound.setVolume(amplitude) self.count = self.count + self.rate / self.duration # print(count) if self.count >= 1 - self.rate / self.duration: self.count = 0 if self.is_accelerating: self.duration = max(200, self.duration * 0.95)
class MainWindow(QtWidgets.QWidget, main_window.Ui_mainWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setupUi(self) self.ICON = QIcon(str(ICON_PATH)) self.setWindowIcon(self.ICON) # BUTTONS self.buttonStart.clicked.connect(self.on_clicked_start) self.buttonSettings.clicked.connect(self.on_clicked_settings) self.buttonStatistics.clicked.connect(self.on_clicked_statistics) self.buttonExit.clicked.connect(QtWidgets.QApplication.instance().quit) self.comboBoxSelectMode.currentIndexChanged.connect( self.on_change_mode) # HIGHSCORES HANDLER self.highscore = highscores.Highscores() self.update_highscores() # DATA AND SETTINGS if DATA_FILE.is_file(): self.load_data_from_file() else: self.data = settings.DEFAULT_DATA self.comboBoxSelectMode.setCurrentIndex( self.data.get("selected_mode", 0)) # SOUND self.set_key_sound(self.get_setting("sound_filename")) # FONT self.inconsolata_bold = self.load_custom_font(str(FONT_PATH)) # Stylesheet is set in the main program after instantiation # Button methods def on_clicked_start(self) -> None: self.make_mode_window(str(self.comboBoxSelectMode.currentText())) self.show_window(self.mode_window, self.isMaximized()) self.mode_window.setStyleSheet(self.get_setting("stylesheet")) self.mode_window.set_colours(self.get_setting("rich_text_colours")) self.hide() def on_clicked_main_menu(self, window: QtWidgets.QWidget) -> None: self.update_highscores() self.show_window(self, window.isMaximized()) window.close() del window def on_clicked_settings(self) -> None: self.make_settings_window() self.show_window(self.settings_window, self.isMaximized()) self.settings_window.setStyleSheet(self.get_setting("stylesheet")) self.hide() def on_clicked_apply(self) -> None: """Executed when apply button in settings window is clicked.""" self.data["settings"] = self.settings_window.get_settings() # Key sound self.set_key_sound(self.get_setting("sound_filename")) # Stylesheet self.settings_window.setStyleSheet(self.get_setting("stylesheet")) self.setStyleSheet(self.get_setting("stylesheet")) # Save self.save_data_to_file() def on_clicked_statistics(self) -> None: self.make_stats_window() self.show_window(self.stats_window, self.isMaximized()) self.stats_window.setStyleSheet(self.get_setting("stylesheet")) self.hide() def on_clicked_reset_daily(self) -> None: """ To be executed when 'Reset today's highscore' is pressed in the stats window. """ self.highscore.delete_daily_highscore() self.update_highscores() self.update_stats_highscores() def on_clicked_reset_all_time(self) -> None: """ To be executed when 'Reset all-time highscore' is pressed in the stats window. """ self.highscore.delete_all_time_highscore() self.update_highscores() self.update_stats_highscores() def on_clicked_reset_all(self) -> None: """ To be executed when 'Reset all highscores' is pressed in the stats window. """ self.highscore.delete_all_highscores() self.update_highscores() self.update_stats_highscores() def on_change_mode(self): """ Saves the selected mode to self.data and pickles self.data so the selection is remembered. """ self.data["selected_mode"] = self.comboBoxSelectMode.currentIndex() self.save_data_to_file() # Helper Methods def get_setting(self, setting: str): """ Convenience method for getting a specific setting from self.data, or a default value. """ return self.data["settings"].get( setting, settings.DEFAULT_SETTINGS.get(setting)) def load_custom_font(self, font: str) -> int: """Adds custom font to QFontDatabase, and returns its corresponding font id.""" return QFontDatabase.addApplicationFont(font) def show_window(self, window: QtWidgets.QWidget, fullscreen: bool) -> None: """ Used to show windows, with the option to have them maximised provided. """ window.show() if fullscreen: window.setWindowState(QtCore.Qt.WindowMaximized) def make_mode_window(self, mode: str) -> None: self.mode_window = type_test.TypingWindow(self.highscore) self.mode_window.set_mode(mode) self.mode_window.setWindowIcon(self.ICON) self.mode_window.buttonMainMenu.clicked.connect( lambda: self.on_clicked_main_menu(self.mode_window)) # Sets key sound if enabled if self.get_setting("play_sound"): self.mode_window.set_key_sound(self.key_sound) def make_settings_window(self) -> None: self.settings_window = settings.SettingsWindow() self.settings_window.setWindowIcon(self.ICON) self.settings_window.buttonMainMenu.clicked.connect( lambda: self.on_clicked_main_menu(self.settings_window)) self.settings_window.buttonApply.clicked.connect(self.on_clicked_apply) # Keystroke sound toggle if self.get_setting("play_sound"): self.settings_window.toggleKeystrokeSound.setChecked(True) # Dark mode toggle if self.get_setting("dark_mode"): self.settings_window.toggleDarkMode.setChecked(True) self.set_settings_sounds_options() self.set_selected_sound_option(self.get_setting("sound_filename")) def make_stats_window(self) -> None: self.stats_window = statistics.StatsWindow() self.stats_window.setWindowIcon(self.ICON) # Update labels self.update_stats_highscores() self.update_stats_days_ago() # Set up graph self.stats_window.set_up_graph(self.highscore.get_stats_dailies(), self.get_setting("graph_colours")) # Connect buttons self.stats_window.buttonMainMenu.clicked.connect( lambda: self.on_clicked_main_menu(self.stats_window)) self.stats_window.buttonResetDaily.clicked.connect( self.on_clicked_reset_daily) self.stats_window.buttonResetAllTime.clicked.connect( self.on_clicked_reset_all_time) self.stats_window.buttonResetAll.clicked.connect( self.on_clicked_reset_all) def update_highscores(self) -> None: self.today_wpm, self.all_time_wpm = self.highscore.get_wpm() def save_data_to_file(self) -> None: """Pickles self.data into a file in the data folder.""" with open(DATA_FILE, "wb") as data_pickle: pickle.dump(self.data, data_pickle) def load_data_from_file(self) -> None: """Sets self.data to the values saved on the data.pkl file.""" with open(DATA_FILE, "rb") as data_pickle: self.data = pickle.load(data_pickle) def get_sounds_list(self) -> list: """Returns a list of the sound files present in the sounds folder.""" return os.listdir(SOUND_FOLDER) def set_settings_sounds_options(self) -> None: """ Sets up options for the dropdown menu to select keystroke sounds in the settings menu. """ for sound_file in self.get_sounds_list(): # Add sound file name to dropdown menu self.settings_window.comboSelectSound.addItem(sound_file) def find_sound_file_index(self, sound_file: str) -> int: """ Returns the index of the given file name within the settings window comboSelectSound object. """ return self.settings_window.comboSelectSound.findText( sound_file, QtCore.Qt.MatchFixedString) def set_selected_sound_option(self, sound_file: str) -> None: """ Sets the selected option for sound file from the settings window's comboSelectSound object to the given sound file name. """ index: int = self.find_sound_file_index(sound_file) if index >= 0: self.settings_window.comboSelectSound.setCurrentIndex(index) def set_key_sound(self, sound_file: str) -> None: """ Sets the given sound file to a QSoundEffect object which will be played on each keystroke in the mode window. """ self.key_sound_path = os.path.join(SOUND_FOLDER, sound_file) self.key_sound_url = QtCore.QUrl.fromLocalFile(self.key_sound_path) self.key_sound = QSoundEffect() self.key_sound.setSource(self.key_sound_url) self.key_sound.setVolume(0.5) self.key_sound.setLoopCount(1) def update_stats_highscores(self) -> None: """Updates highscores displayed in the stats window.""" self.stats_window.labelTodayScore.setText(f"{self.today_wpm} WPM") self.stats_window.labelAllTimeScore.setText(f"{self.all_time_wpm} WPM") def update_stats_days_ago(self) -> None: """ Updates the labelDaysAgo element in the stats window with the number of days since the all-time highscore was set. """ self.stats_window.update_days_ago(self.highscore.days_since_set())
app = QApplication(sys.argv) widget = QWidget() widget.setGeometry(200, 200, 500, 500) layout = QHBoxLayout() widget.setLayout(layout) button = QPushButton("test") layout.addWidget(button) # audio sound_file = "../Sounds/alarms/al1.wav" sound_file = "../Sounds/alarms/al6-low.wav" #sound_file = "../Sounds/alarms/al6-medium.wav" #sound_file = "../Sounds/alarms/al6-high.wav" sound = QSoundEffect() sound.setSource(QUrl.fromLocalFile(sound_file)) sound.setLoopCount(QSoundEffect.Infinite) #animate label background and sound count = 0 def update_anim(): global count #logarithmic (check with perception of luminosity/brighness) #val = math.log(count + 1) #linear val = 1 - count #sinus #val = math.sin(count*2*math.pi) / 2 + 0.5 #print(val) alpha = val * 100 + 155