我们从Python开源项目中,提取了以下30个代码示例,用于说明如何使用PyQt5.QtCore.Qt.KeepAspectRatio()。
def drawUI(self, song_name="blank", song_artist="blank"): self.art = QLabel(self) self.art.resize(50, 50) #self.art.setPixmap(QPixmap("".join([song_name,'.jpg'])).scaled(50, 50, Qt.KeepAspectRatio, Qt.SmoothTransformation)) self.song_label = QLabel(self) self.song_label.setText(song_name) self.song_label.setStyleSheet("border-image: rgba(0, 0, 0, 0); color: rgba(255, 255, 255); font-size: 12pt; font-weight: 600;") self.song_label.move(60, 10) self.song_label.resize(180, 15) self.artist_label = QLabel(self) self.artist_label.setText(song_artist) self.artist_label.setStyleSheet("border-image: rgba(0, 0, 0, 0); color: rgba(255, 255, 255); font-size: 12pt; font-weight: 200;") self.artist_label.move(60, 25) self.artist_label.resize(180, 15)
def load_image(self, image): """ Call this to load a new image from the provide QImage into this HomographyView's scene. The image's top left corner will be placed at (0,0) in the scene. """ self.scene_image = image new_scene = HomographyScene(self) pmap = QtGui.QPixmap().fromImage(image) pmapitem = new_scene.addPixmap(pmap) new_scene.register_pixmap(pmapitem) new_scene.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(0, 0, 0))) self.setScene(new_scene) self.fitInView(0, 0, pmap.width(), pmap.height(), Qt.KeepAspectRatio) self.show() self.image_loaded = True
def drawUI(self): self.play = QPushButton(self) self.play.setIconSize(QSize(40, 40)) self.play.setIcon(QIcon(QPixmap("play.png").scaled(40, 40, Qt.KeepAspectRatio, Qt.SmoothTransformation))) self.play.setStyleSheet("background-color: rgba(0, 0, 0, 0);") self.play.clicked.connect(lambda:self.playpause(self.play)) self.resize(50, 50)
def playpause(self, btn): if self.playing: os.system("""osascript -e 'tell application "iTunes" to pause'""") self.playing = False btn.setIcon(QIcon(QPixmap("play.png").scaled(40, 40, Qt.KeepAspectRatio, Qt.SmoothTransformation))) else: os.system("""osascript -e 'tell application "iTunes" to play'""") self.playing = True btn.setIcon(QIcon(QPixmap("pause.png").scaled(40, 40, Qt.KeepAspectRatio, Qt.SmoothTransformation)))
def drawUI(self): self.play = QPushButton(self) self.play.setIconSize(QSize(40, 20)) self.play.setIcon(QIcon(QPixmap("fastforward.png").scaled(40, 20, Qt.KeepAspectRatio, Qt.SmoothTransformation))) self.play.setStyleSheet("background-color: rgba(0, 0, 0, 0);") self.play.clicked.connect(lambda:self.forward()) self.resize(80, 40)
def drawUI(self): self.play = QPushButton(self) self.play.setIconSize(QSize(40, 20)) self.play.setIcon(QIcon(QPixmap("rewind.png").scaled(40, 20, Qt.KeepAspectRatio, Qt.SmoothTransformation))) self.play.setStyleSheet("background-color: rgba(0, 0, 0, 0);") self.play.clicked.connect(lambda:self.rewind()) self.resize(80, 40)
def print_(self): dialog = QPrintDialog(self.printer, self) if dialog.exec_(): painter = QPainter(self.printer) rect = painter.viewport() size = self.imageLabel.pixmap().size() size.scale(rect.size(), Qt.KeepAspectRatio) painter.setViewport(rect.x(), rect.y(), size.width(), size.height()) painter.setWindow(self.imageLabel.pixmap().rect()) painter.drawPixmap(0, 0, self.imageLabel.pixmap())
def print_(self): printer = QPrinter(QPrinter.HighResolution) printDialog = QPrintDialog(printer, self) if printDialog.exec_() == QPrintDialog.Accepted: painter = QPainter(printer) rect = painter.viewport() size = self.image.size() size.scale(rect.size(), Qt.KeepAspectRatio) painter.setViewport(rect.x(), rect.y(), size.width(), size.height()) painter.setWindow(self.image.rect()) painter.drawImage(0, 0, self.image) painter.end()
def resizeEvent(self, event): scaledSize = self.originalPixmap.size() scaledSize.scale(self.screenshotLabel.size(), Qt.KeepAspectRatio) if not self.screenshotLabel.pixmap() or scaledSize != self.screenshotLabel.pixmap().size(): self.updateScreenshotLabel()
def updateScreenshotLabel(self): self.screenshotLabel.setPixmap(self.originalPixmap.scaled( self.screenshotLabel.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
def processCapturedImage(self, requestId, img): scaledImage = img.scaled(self.ui.viewfinder.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation) self.ui.lastImagePreviewLabel.setPixmap(QPixmap.fromImage(scaledImage)) self.displayCapturedImage() QTimer.singleShot(4000, self.displayViewfinder)
def scale_image_to_aspect_fit_label(self, qpixmap): if qpixmap: scaled_image = qpixmap.scaled(self.image_view.size(), Qt.KeepAspectRatio | Qt.SmoothTransformation) self.image_view.setPixmap(scaled_image)
def showzcpqr(self): fd = open('./hidden_service/hostname') oniondom = fd.readline().split()[0] fd.close() username = str(self.line_user.text()) password = str(self.line_password.text()) img = qrcode.make(username+':'+password+'@'+oniondom) img.save('qrcode.png', 'PNG') qrc = QPixmap('qrcode.png') os.remove('qrcode.png') self.onionlabelname.setText(username+':'+password+'@'+oniondom) self.onionlabelname.show() self.onionlabel.setPixmap(qrc.scaled(self.onionlabel.size(), Qt.KeepAspectRatio)) self.onionlabel.show()
def showorbotqr(self): fd = open('./hidden_service/hostname') line = fd.readline() oniondom =line.split()[0] cookie = line.split()[1] fd.close() jsonstring = '{'+'"auth_cookie_value": "{}", "domain":"{}"'.format(cookie, oniondom)+'}' img = qrcode.make(jsonstring) img.save('qrcode.png', 'PNG') qrc = QPixmap('qrcode.png') os.remove('qrcode.png') self.onionlabelname.setText(jsonstring) self.onionlabelname.show() self.onionlabel.setPixmap(qrc.scaled(self.onionlabel.size(), Qt.KeepAspectRatio)) self.onionlabel.show()
def geneartereceiveqr(self): try: address = self.receiveaddresses[self.listaddresses_receive.currentIndex().row()] except: address = '' amount = self.line_receiveamount.text() comment = self.line_receivedesc.text() if address and amount: if comment: try: string = 'zcash:{}?amount={}&message={}'.format(address,amount,comment) except: self.label_qrreceive.hide() self.label_textreceive.hide() return else: string = 'zcash:{}?amount={}'.format(address,amount) img = qrcode.make(string) img.save('qrcode.png', 'PNG') qrc = QPixmap('qrcode.png') os.remove('qrcode.png') self.label_textreceive.setText(string) self.label_textreceive.show() self.label_qrreceive.setPixmap(qrc.scaled(self.onionlabel.size(), Qt.KeepAspectRatio)) self.label_qrreceive.show() else: self.label_qrreceive.hide() self.label_textreceive.hide()
def resizeEvent(self, resize_event): self.fitInView(QRectF(0, 0, self.w, self.h), Qt.KeepAspectRatio)
def load_image(self, image): """ Call this to load a new image from the provide QImage into this HomographyView's scene. The image's top left corner will be placed at (0,0) in the scene. """ self.scene_image = image new_scene = QtWidgets.QGraphicsScene(self) pmap = QtGui.QPixmap().fromImage(image) pmapitem = new_scene.addPixmap(pmap) new_scene.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(0, 0, 0))) self.setScene(new_scene) self.fitInView(0, 0, pmap.width(), pmap.height(), Qt.KeepAspectRatio) self.show() self.image_loaded = True
def set_scalable_pixmap(self, pixmap): self.original_pixmap = pixmap self.setPixmap(self.original_pixmap.scaled(self.size(), Qt.KeepAspectRatio))
def resizeEvent(self, event): if self.pixmap() and self.original_pixmap: self.setPixmap(self.original_pixmap.scaled(event.size(), Qt.KeepAspectRatio))
def drawUI(self): self.alpha_rect = QLabel(self) self.alpha_rect.setPixmap(QPixmap("black_alpha.png").scaled(320, 480, Qt.KeepAspectRatio, Qt.SmoothTransformation)) self.current_song = SongWidget(self) self.current_song.drawUI() self.current_song.move(40, 40) self.line1 = QLabel(self) self.line1.setPixmap(QPixmap('line.png').scaled(300, 1)) self.line1.move(10, 120) self.time_line = QSlider(Qt.Horizontal, self) self.time_line.setGeometry(40, 135, 240, 4) self.time_line.setMaximum(157) self.time_line.setMinimum(0) self.time_line.setValue(0) self.time_line.setStyleSheet("""QSlider::groove:horizontal {background-color: rgba(128, 128, 128, 0.5); border: 1px solid rgba(128, 128, 128, 0.5); border-radius: 2px; height:4px;} QSlider::handle:horizontal {border: 1px solid #fff;width: 4px;height: 4px;border-radius: 2px; background-color: #fff;} QSlider::sub-page:horizontal {background-color: #fff;} """) self.time_line.sliderReleased.connect(self.changeTime) self.rewind = RewindBtn(self) self.rewind.move(50, 175) self.play = StateBtn(self) self.play.move(135, 165) self.fastforward = ForwardBtn(self) self.fastforward.move(220, 175) self.line2 = QLabel(self) self.line2.setPixmap(QPixmap('line.png').scaled(300, 1)) self.line2.move(10, 240) self.r_song.append(SongWidget(self)) self.r_song[0].drawUI() self.r_song[0].move(40, 270) self.r_song.append(SongWidget(self)) self.r_song[1].drawUI() self.r_song[1].move(40, 330) self.r_song.append(SongWidget(self)) self.r_song[2].drawUI()#, song_loc="/Users/vidursatija/Music/iTunes/iTunes Media/Music/Halsey/hopeless fountain kingdom (Deluxe)/01 The Prologue.mp3") self.r_song[2].move(40, 390) self.timer = QTimer(self) self.timer.timeout.connect(self.updateLabels) self.timer.start(1000)