I want to be able to detect if the slider has moved to a new position and then do some actions in another function called sl. Here is my code:
import sys
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QScrollBar, QMainWindow, QMenu, QWidget, QGridLayout, \
QMessageBox, QListWidget, QPlainTextEdit, QTableWidget, QTableWidgetItem, QHeaderView
from PyQt5.QtWidgets import QMenuBar, QHBoxLayout, QVBoxLayout, QSlider, QPushButton, QDial, QBoxLayout, QSpacerItem
from PyQt5.QtGui import QFont, QColor, QPixmap, QResizeEvent, QPen
import PyQt5.QtGui as QtGui
import PyQt5.QtCore as QtCore
class ControlWidget(QWidget):
def __init__(self):
QWidget.__init__(self)
self.layout = QVBoxLayout()
self.layout.setContentsMargins(10, 10, 10, 10)
widget3 = QLabel("Manual")
big_font = QFont('Arial')
big_font.setPointSize(15)
widget3.setFont(big_font)
self.layout.addWidget(widget3, stretch=1)
widget2 = ManualWidget()
self.layout.addWidget(widget2, stretch=4)
self.setLayout(self.layout)
class ManualWidget(QWidget):
def __init__(self):
QWidget.__init__(self)
self.layout = QHBoxLayout()
right_widget = QWidget()
right_layout = QVBoxLayout()
right_top_widget = QWidget()
self.sld1 = self.slider('V1:', 0, 100)
right_top_layout = QVBoxLayout()
right_top_layout.setContentsMargins(0, 0, 0, 0)
right_top_layout.addWidget(self.sld1, stretch=1)
right_top_widget.setLayout(right_top_layout)
right_layout.addWidget(right_top_widget)
right_widget.setLayout(right_layout)
self.layout.addWidget(right_widget, stretch=1)
self.setLayout(self.layout)
self.layout.addWidget(self.name)
self.layout.addWidget(self.label)
self.layout.addWidget(self.slider)
print(self.sl())
def slider(self, name, low, high, step=10):
self.name = QLabel(str(name))
self.label = QLabel(str(low))
self.slider = QSlider(Qt.Horizontal, self)
self.slider.setMinimum(low)
self.slider.setMaximum(high*step)
self.slider.setValue(low)
self.slider.valueChanged.connect(self.change_value)
self.action = False
self.slider.sliderMoved.connect(self.act1)
def change_value(self):
self.set_point = (float(self.slider.value())) / 10
self.label.setText(str(self.set_point))
def act1(self):
self.action = True
return self.action
def sl(self):
if self.action == True:
x = 3
else:
x = 6
return x
class MainWidget(QWidget):
def __init__(self):
QWidget.__init__(self)
self.layout = QVBoxLayout()
self.layout.setContentsMargins(0, 0, 0, 0)
big_font = QFont('Arial')
big_font.setPointSize(10)
bottom_widget = ControlWidget()
self.layout.addWidget(bottom_widget, stretch=10)
self.setLayout(self.layout)
class Window(QMainWindow):
def __init__(self, widget):
QMainWindow.__init__(self)
self.setWindowTitle("Flow test rig")
self.menu = self.menuBar()
self.setCentralWidget(widget)
self.status = self.statusBar()
widget.parent = self
app = QApplication(sys.argv)
main_widget = MainWidget()
win = Window(main_widget)
win.show()
sys.exit(app.exec_())
I tried to detect it with the act1 function, however, self.action is always False, or when it becomes True, it does not reset to False after the first move of the slider. I appreciate it if someone would help me.
Related
I'm trying to integrate QFontComboBox within Qmenu.
I try to make two things happen when selecting a particular font from the menu:
The Qmenu will close.
print the selected font.
from PyQt5.QtCore import QObject
from PyQt5.QtGui import QIcon, QFont, QCursor
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QFontComboBox, QWidgetAction, QMenu, QPushButton
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Pyside2 FontCombo Box")
self.setGeometry(300,200,300,250)
self.setFontBox()
self.setIcon()
self.show()
def setIcon(self):
appIcon = QIcon("icon.png")
self.setWindowIcon(appIcon)
def setFontBox(self):
self.font_button = QPushButton(self)
self.font_button.setFixedWidth(300)
self.font_button.clicked.connect(lambda: self.setFontmenu())
vbox = QVBoxLayout()
vbox.addWidget(self.font_button)
self.setLayout(vbox)
def setFontmenu(self):
font_menu = QMenu()
font_submenu = QFontComboBox()
font_submenu.setCurrentFont(QFont("Arial"))
objectTest = QObject()
widget = QWidgetAction(objectTest)
widget.setDefaultWidget(font_submenu)
font_menu.addAction(widget)
font_menu.exec_(QCursor.pos())
menu = font_menu
menu.addSeparator()
font_submenu.showPopup()
font_submenu.setFocus()
font_submenu.currentFontChanged.connect(self._changed)
def _changed(self):
font = self.currentFont().family()
print(font)
return
myapp = QApplication(sys.argv)
window = Window()
myapp.exec_()
sys.exit()
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QFileDialog, QPushButton, QLineEdit,QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import *
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QDialog, QVBoxLayout
import Updated_encrypt
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.title = 'Encrypt/Decrypt'
self.top = 200
self.left = 500
self.width = 400
self.height = 300
self.InitWindow()
def InitWindow(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
vbox = QVBoxLayout()
self.button1 = QPushButton('Encrypt')
self.button1.clicked.connect(self.openSecondDialog)
self.button2 = QPushButton('Decrypt')
vbox.addWidget(self.button1)
vbox.addWidget(self.button2)
self.setLayout(vbox)
self.show()
def openSecondDialog(self):
hbox = QVBoxLayout()
mydialog = QDialog(self)
mydialog.show()
self.button2 = QPushButton('Check Image')
self.button2.clicked.connect(self.getImage)
hbox.addWidget(self.button2)
self.setLayout(hbox)
self.show()
def getImage(self):
hbox = QHBoxLayout()
file_Name = QFileDialog.getOpenFileName(self,
'OpenFile',
'',
'')
image_path = file_Name[0]
updatedImage = Updated_encrypt.decrypt(image_path, 123)
pixmap = QPixmap(updatedImage)
self.label.setPixmap(QPixmap(pixmap))
self.resize(pixmap.width(), pixmap.height())
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
I have my code set up to implement an algorithm to modify an image when my I select it from my pop up dialogue. What I am trying to accomplish is for the image to pop up in a separate window when I click the encrypt button. I can't seem to get anything to pop up in the separate window aside from the window itself. Any help would be appreciated.
You have at least the following errors:
The "hbox" created is being added to the window and not to the QDialog: self.setLayout(hbox), it must be mydialog.setLayout(hbox).
Do not use the same name for 2 different objects as they can cause problems, in your case there are 2 QPushButton assigned to the variable "self.button2".
You try to use the variable "self.label" but never believe it.
Considering the above we can make the following improvements:
Use more descriptive names to easily distinguish their function.
If you are going to have a window that has a different objective, it is better to create a class.
The above avoid the indicated problems, considering the above the solution is:
import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import (
QApplication,
QDialog,
QFileDialog,
QHBoxLayout,
QLabel,
QPushButton,
QVBoxLayout,
QWidget,
)
import Updated_encrypt
class Dialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.load_image_btn = QPushButton("Check Image")
self.load_image_btn.clicked.connect(self.load_image)
self.image_lbl = QLabel()
lay = QVBoxLayout(self)
lay.addWidget(self.load_image_btn)
lay.addWidget(self.image_lbl)
def load_image(self):
image_path, _ = QFileDialog.getOpenFileName(self, "OpenFile", "", "")
if image_path:
updatedImage = Updated_encrypt.decrypt(image_path, 123)
pixmap = QPixmap(updatedImage)
self.image_lbl.setPixmap(QPixmap(pixmap))
class Window(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.title = "Encrypt/Decrypt"
self.InitWindow()
def InitWindow(self):
self.setWindowTitle(self.title)
self.setGeometry(200, 500, 400, 300)
self.encrypt_btn = QPushButton("Encrypt")
self.encrypt_btn.clicked.connect(self.openSecondDialog)
self.decrypt_btn = QPushButton("Decrypt")
vbox = QVBoxLayout(self)
vbox.addWidget(self.encrypt_btn)
vbox.addWidget(self.decrypt_btn)
def openSecondDialog(self):
dialog = Dialog(self)
dialog.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
This code moves the text from left to right using PyQt4. My question is, are there other ways to do it?
# -*-coding:utf8 -*-
from PyQt4.QtGui import (QApplication, QGraphicsView, QPainter, QColor, QWidget,
QGraphicsScene, QFont, QLabel, QGraphicsSimpleTextItem,
QBrush, QLinearGradient, QPixmap, QGraphicsTextItem, QPushButton,
QVBoxLayout, QTextDocument)
from PyQt4.QtCore import (QObject, QPointF, QPropertyAnimation,
pyqtProperty, Qt, QAbstractAnimation, QRectF)
import sys
data = [u"I want to make text within an ad bar that moves as there is on the TV screen like this"]
class Text(QObject):
def __init__(self):
super(Text, self).__init__()
doc = QTextDocument("%s" % data[0])
doc.setDefaultStyleSheet(
"body { color : green; background-color : black }")
doc.setDefaultFont(QFont("Times", 20, QFont.Bold))
self.Text_item = QGraphicsTextItem()
self.Text_item.setDocument(doc)
def _set_pos(self, pos):
self.Text_item.setPos(pos)
pos = pyqtProperty(QPointF, fset=_set_pos)
class Example(QGraphicsView):
def __init__(self):
super(Example, self).__init__()
self.initView()
def initView(self):
self.text = Text()
self.anim = QPropertyAnimation(self.text, b"pos")
self.anim.setDuration(19000)
self.anim.setLoopCount(4)
self.anim.setStartValue(QPointF(-1850.0, 450.0))
self.anim.setEndValue(QPointF(800.0, 450.0))
self.scene = QGraphicsScene(self)
self.scene.setSceneRect(0, 0, 300, 300)
self.scene.addItem(self.text.Text_item)
self.setScene(self.scene)
self.setWindowTitle("Text moveing")
self.setRenderHint(QPainter.Antialiasing)
self.anim.start(QAbstractAnimation.DeleteWhenStopped)
if __name__ == '__main__':
app = QApplication(sys.argv)
view = Example()
rect = QApplication.desktop().availableGeometry()
view.resize(int(rect.width() * 0.75), int(rect.height() * 0.9))
view.show()
sys.exit(app.exec_())
I have these 2 scripts. First contains ui using pyqt5,this contains some textbox and buttons. The second one contains the functions of the buttons( i mean what it will do).For now I just want to display in the terminal on what is written in the textbox. The problem is i can't get/print/display the input text in the textbox using the second py. I have this button(upload button) which must display this text in the terminal when click.
I have tried to import the second py to first. Then i connect the button.
butUpld.clicked.connect(py2.up)
for the second py i also connect it to the first one
This is the first py file name py1.py
import os, glob, sys, time
import py2
from PyQt5.Qt import QApplication, QClipboard, QUrl
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QMainWindow, QWidget, QPlainTextEdit, QLabel, QGridLayout, QPushButton, QLineEdit, QTextEdit, QMessageBox, QComboBox, QTabWidget, QFormLayout, QHBoxLayout, QRadioButton, QCheckBox, QVBoxLayout, QFileDialog, QButtonGroup
from PyQt5.QtCore import pyqtSlot, Qt
qt_app = QApplication(sys.argv)
class Layoutexe(QTabWidget):
def __init__(self, parent = None):
super(Layoutexe, self).__init__(parent)
self.tab1 = QWidget()
self.tab2 = QWidget()
self.addTab(self.tab1," ")
self.addTab(self.tab2," ")
self.tab1UI()
self.tab2UI()
self.setWindowTitle(" ")
self.setFixedSize(900,1000)
def tab1UI(self):
vbox = QVBoxLayout()
hbox = QHBoxLayout()
hbox1 = QHBoxLayout()
hbox2 = QHBoxLayout()
hbox3 = QHBoxLayout()
labelPath = QLabel("PATH:")
tboxPath = QLineEdit(self)
butSrch = QPushButton("Search", self)
butClr = QPushButton("Clear", self)
hbox.addStretch()
hbox.addWidget(labelPath)
hbox.addWidget(tboxPath)
hbox.addWidget(butSrch)
hbox.addStretch()
hbox.addWidget(butClr)
vbox.addLayout(hbox)
radbutLst = QRadioButton("Latest")
radbutMnl = QRadioButton("Manual")
hbox1.addWidget(radbutLst)
hbox1.addWidget(radbutMnl)
hbox1.addStretch()
vbox.addLayout(hbox1)
labelEV = QLabel("Event:")
tboxEV = QLineEdit(self)
hbox2.addWidget(labelEV)
hbox2.addWidget(tboxEV)
hbox2.addStretch()
vbox.addLayout(hbox2)
butUpld = QPushButton("UPLOAD", self)
hbox3.addStretch()
hbox3.addWidget(butUpld)
hbox3.addStretch()
vbox.addLayout(hbox3)
vbox.addStretch()
self.tab1.setLayout(vbox)
butUpld.clicked.connect(py2.up)
def tab2UI(self):
vbox = QVBoxLayout()
layout = QHBoxLayout()
def run(self):
self.show()
qt_app.exec_()
app = Layoutexe()
app.run()
this the second py file named py2.py
import os, glob
import py1
def up():
a = gui.rep.tab1U.tboxEV
print(a.text())
Try it:
py1.py
import sys
#import os, glob, time
from PyQt5.Qt import QApplication, QClipboard, QUrl
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QMainWindow, QWidget, QPlainTextEdit, QLabel, QGridLayout, QPushButton, QLineEdit, QTextEdit, QMessageBox, QComboBox, QTabWidget, QFormLayout, QHBoxLayout, QRadioButton, QCheckBox, QVBoxLayout, QFileDialog, QButtonGroup
from PyQt5.QtCore import pyqtSlot, Qt
import py2
qt_app = QApplication(sys.argv)
class Layoutexe(QTabWidget):
def __init__(self, parent = None):
super(Layoutexe, self).__init__(parent)
self.tab1 = QWidget()
self.tab2 = QWidget()
self.addTab(self.tab1," ")
self.addTab(self.tab2," ")
self.tab1UI()
self.tab2UI()
self.setWindowTitle(" ")
self.setFixedSize(900,1000)
def tab1UI(self):
vbox = QVBoxLayout()
hbox = QHBoxLayout()
hbox1 = QHBoxLayout()
hbox2 = QHBoxLayout()
hbox3 = QHBoxLayout()
labelPath = QLabel("PATH:")
tboxPath = QLineEdit(self)
butSrch = QPushButton("Search", self)
butClr = QPushButton("Clear", self)
hbox.addStretch()
hbox.addWidget(labelPath)
hbox.addWidget(tboxPath)
hbox.addWidget(butSrch)
hbox.addStretch()
hbox.addWidget(butClr)
vbox.addLayout(hbox)
radbutLst = QRadioButton("Latest")
radbutMnl = QRadioButton("Manual")
hbox1.addWidget(radbutLst)
hbox1.addWidget(radbutMnl)
hbox1.addStretch()
vbox.addLayout(hbox1)
labelEV = QLabel("Event:")
self.tboxEV = QLineEdit(self)
hbox2.addWidget(labelEV)
hbox2.addWidget(self.tboxEV)
hbox2.addStretch()
vbox.addLayout(hbox2)
butUpld = QPushButton("UPLOAD", self)
hbox3.addStretch()
hbox3.addWidget(butUpld)
hbox3.addStretch()
vbox.addLayout(hbox3)
vbox.addStretch()
self.tab1.setLayout(vbox)
butUpld.clicked.connect(lambda _, s=self: py2.up(s)) # +++
def tab2UI(self):
vbox = QVBoxLayout()
layout = QHBoxLayout()
def run(self):
self.show()
qt_app.exec_()
app = Layoutexe()
app.run()
py2.py
#import os, glob
#import py1
def up(self):
# a = gui.rep.tab1U.tboxEV
a = self.tboxEV
print(a.text())
How do I print in multiple QLabel when I click the PushButton?, because it only works in Quantity I also want it in Item Name and Price. I tried putting multiple print_click(self) it wont work it say redefinition of unused 'print_clink'. Thanks in advance
My Code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSlot`
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.ItemName = QLabel('Item Name:')
self.Item_Line = QLabel('')
self.Item_Name = QLineEdit('')
self.PriceName = QLabel('Price:')
self.Price_Line = QLabel('')
self.Price_Name = QLineEdit('')
self.QuantityName = QLabel('Quantity:')
self.Quantity_Line = QLabel('0')
self.Quantity_Name = QLineEdit()
self.Update_button = QPushButton("Update")
self.Update_button.clicked.connect(self.print_click)
self.Clear_button = QPushButton("Clear")
self.Clear_button.clicked.connect(self.clear_click)
hbox = QHBoxLayout(self)
hbox.addWidget(self.ItemName)
hbox.addWidget(self.Item_Name)
hbox.addWidget(self.PriceName)
hbox.addWidget(self.Price_Name)
hbox.addWidget(self.QuantityName)
hbox.addWidget(self.Quantity_Line)
hbox.addWidget(self.Quantity_Name)
hbox.addWidget(self.Update_button)
hbox.addWidget(self.Clear_button)
self.show()
self.Clear_button.clicked.connect(self.Item_Line.clear)
self.Clear_button.clicked.connect(self.Item_Name.clear)
self.Clear_button.clicked.connect(self.Price_Line.clear)
self.Clear_button.clicked.connect(self.Price_Name.clear)
self.Clear_button.clicked.connect(self.Quantity_Line.clear)
self.Clear_button.clicked.connect(self.Quantity_Name.clear)
#pyqtSlot()
def print_click(self):
self.Quantity_Line.setText(self.Quantity_Name.text())
def clear_click(self):
self.Quantity_Line(self.Quantity_Name.text(''))
return self.Quantity
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
I'm not completely sure of the expected result but I guess there are some mistakes and redundancies in your code :
the Price_Line and Item_Line weren't added to the QHBoxLayout
the method print_click wasn't setting the text from Price_Name and Item_Name on the respective Price_Line and Item_Line.
the clear_click method wasn't really useful as you already connected the clear method of every other elements on that button.
The following code is adapted from yours, paying attention to the points mentioned above :
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSlot
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.ItemName = QLabel('Item Name:')
self.Item_Line = QLabel('')
self.Item_Name = QLineEdit()
self.PriceName = QLabel('Price:')
self.Price_Line = QLabel('')
self.Price_Name = QLineEdit()
self.QuantityName = QLabel('Quantity:')
self.Quantity_Line = QLabel('0')
self.Quantity_Name = QLineEdit()
self.Update_button = QPushButton("Update")
self.Update_button.clicked.connect(self.print_click)
self.Clear_button = QPushButton("Clear")
hbox = QHBoxLayout(self)
hbox.addWidget(self.ItemName)
hbox.addWidget(self.Item_Line)
hbox.addWidget(self.Item_Name)
hbox.addWidget(self.PriceName)
hbox.addWidget(self.Price_Line)
hbox.addWidget(self.Price_Name)
hbox.addWidget(self.QuantityName)
hbox.addWidget(self.Quantity_Line)
hbox.addWidget(self.Quantity_Name)
hbox.addWidget(self.Update_button)
hbox.addWidget(self.Clear_button)
self.show()
self.Clear_button.clicked.connect(self.Item_Line.clear)
self.Clear_button.clicked.connect(self.Item_Name.clear)
self.Clear_button.clicked.connect(self.Price_Line.clear)
self.Clear_button.clicked.connect(self.Price_Name.clear)
self.Clear_button.clicked.connect(self.Quantity_Line.clear)
self.Clear_button.clicked.connect(self.Quantity_Name.clear)
def print_click(self):
self.Price_Line.setText(self.Price_Name.text())
self.Item_Line.setText(self.Item_Name.text())
self.Quantity_Line.setText(self.Quantity_Name.text())
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())