Below code, image loading from the specific Path and placing on the bottom of the Window.Here i need to load image from the top.So for that i need to change the placement of the Tab Widget(Tab01,Tab02,Tab03).
I am unable to load the image from the top and unable to change the position of the Tab Widget.How to do this and how to load the Image from the top.
import re,sys,os
from PyQt4 import QtGui,QtCore
class projs(QtGui.QWidget):
def __init__(self, parent=None):
super(projs,self).__init__(parent)
self.setWindowTitle(" V1.0 ")
tab_widget = QtGui.QTabWidget()
proj = QtGui.QWidget()
asset = QtGui.QWidget()
shots = QtGui.QWidget()
tab_widget.addTab(proj, "Tab01")
tab_widget.addTab(asset, "Tab02")
tab_widget.addTab(shots, "Tab03")
self.projPathLabel = QtGui.QLabel("Project Path ",proj)
self.projPathLabel.setGeometry(10,20,100,25)
self.assetPathLabel = QtGui.QLabel("Project ",asset)
self.assetPathLabel.setGeometry(20,20,100,25)
self.shotPathLabel = QtGui.QLabel("Project ",shots)
self.shotPathLabel.setGeometry(20,20,100,25)
vbox = QtGui.QVBoxLayout()
vbox.addWidget(tab_widget)
self.setLayout(vbox)
p1_vbox = QtGui.QVBoxLayout(proj)
proj.setLayout(p1_vbox)
p2_vbox = QtGui.QVBoxLayout(asset)
asset.setLayout(p2_vbox)
p3_vbox = QtGui.QVBoxLayout(shots)
shots.setLayout(p3_vbox)
vuvupic = QtGui.QPixmap("C:\Users\name\Documents\config\Logo2.jpg")
label = QtGui.QLabel(self)
label.setPixmap(vuvupic)
vbox.addWidget(label)
app = QtGui.QApplication([])
win = projs()
win.resize(410,390)
win.show()
sys.exit(app.exec_())
By "from the top," do you mean that you want to have the image placed above the the TabWidget? QLayouts are ordered in the same order of vbox.addWidget() calls, which means you could just reorder the function calls to put the picture on top.
import re,sys,os
from PyQt4 import QtGui,QtCore
class projs(QtGui.QWidget):
def __init__(self, parent=None):
super(projs,self).__init__(parent)
self.setWindowTitle(" V1.0 ")
tab_widget = QtGui.QTabWidget()
proj = QtGui.QWidget()
asset = QtGui.QWidget()
shots = QtGui.QWidget()
tab_widget.addTab(proj, "Tab01")
tab_widget.addTab(asset, "Tab02")
tab_widget.addTab(shots, "Tab03")
self.projPathLabel = QtGui.QLabel("Project Path ",proj)
self.projPathLabel.setGeometry(10,20,100,25)
self.assetPathLabel = QtGui.QLabel("Project ",asset)
self.assetPathLabel.setGeometry(20,20,100,25)
self.shotPathLabel = QtGui.QLabel("Project ",shots)
self.shotPathLabel.setGeometry(20,20,100,25)
vbox = QtGui.QVBoxLayout()
vuvupic = QtGui.QPixmap("C:\Users\name\Documents\config\Logo2.jpg")
label = QtGui.QLabel(self)
label.setPixmap(vuvupic)
vbox.addWidget(label)
vbox.addWidget(tab_widget)
self.setLayout(vbox)
p1_vbox = QtGui.QVBoxLayout(proj)
proj.setLayout(p1_vbox)
p2_vbox = QtGui.QVBoxLayout(asset)
asset.setLayout(p2_vbox)
p3_vbox = QtGui.QVBoxLayout(shots)
shots.setLayout(p3_vbox)
app = QtGui.QApplication([])
win = projs()
win.resize(410,390)
win.show()
sys.exit(app.exec_())
Also, for moderately sized project, you may want to look into using QtDesigner to build layouts. Googling "pyqt designer" will give you plenty of blogs and tutorials for getting started.
Related
I'm trying to make a toolbox widget that will do various different things. But I'm having trouble with the layout management regarding the QScrollArea. Following the stripped version of the code I have:
from PyQt5 import QtWidgets
import sys
class MyScrollWidget(QtWidgets.QWidget):
def __init__(self):
super(MyScrollWidget, self).__init__()
scrollArea = QtWidgets.QScrollArea(self)
top_widget = QtWidgets.QWidget()
top_layout = QtWidgets.QVBoxLayout()
for i in range(10):
group_box = QtWidgets.QGroupBox()
group_box.setTitle('GroupBox For Item {0}'.format(i))
layout = QtWidgets.QHBoxLayout(group_box)
label = QtWidgets.QLabel()
label.setText('Label For Item {0}'.format(i))
layout.addWidget(label)
push_button = QtWidgets.QPushButton(group_box)
push_button.setText('Run Button')
push_button.setFixedSize(100, 32)
layout.addWidget(push_button)
group_box.setLayout(layout)
top_layout.addWidget(group_box)
top_widget.setLayout(top_layout)
scrollArea.setWidget(top_widget)
self.resize(200, 500)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
widget = MyScrollWidget()
widget.show()
sys.exit(app.exec_())
But this only gives me a small fixed subsection of the widget that scrolled. But what I really wants is the whole widget to be scrollable if the widget/window is smaller than the total size of all the group boxes. I.e I'd like the widget to be used as if it was all fixed width, but if the user resized the widget smaller than that, it would scroll appropriately. I've tried various different methods with no good results so now I'm deferring to those who have more experience with layout management than I. Thank you for your time.
You have to set the scrollArea to MyScrollWidget using a layout.
from PyQt5 import QtWidgets
import sys
class MyScrollWidget(QtWidgets.QWidget):
def __init__(self):
super(MyScrollWidget, self).__init__()
lay = QtWidgets.QVBoxLayout(self)
scrollArea = QtWidgets.QScrollArea()
lay.addWidget(scrollArea)
top_widget = QtWidgets.QWidget()
top_layout = QtWidgets.QVBoxLayout()
for i in range(10):
group_box = QtWidgets.QGroupBox()
group_box.setTitle('GroupBox For Item {0}'.format(i))
layout = QtWidgets.QHBoxLayout(group_box)
label = QtWidgets.QLabel()
label.setText('Label For Item {0}'.format(i))
layout.addWidget(label)
push_button = QtWidgets.QPushButton(group_box)
push_button.setText('Run Button')
push_button.setFixedSize(100, 32)
layout.addWidget(push_button)
top_layout.addWidget(group_box)
top_widget.setLayout(top_layout)
scrollArea.setWidget(top_widget)
self.resize(200, 500)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
widget = MyScrollWidget()
widget.show()
sys.exit(app.exec_())
I have a problem; How can i get the path (directory) from "QListWidget" selected item.
When I 'am using os.path, this gives me Autodesk Maya Directory. But i need directory of my selected item from QListWidget item.
from PySide.QtGui import *
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
import os
import maya.cmds as cm
Import maya commands from maya as cm. This not include python source files.
class projectManagerMainWindow(MayaQWidgetDockableMixin, QWidget):
def __init__(self):
super(projectManagerMainWindow, self).__init__()
self.selectProject = QComboBox()
self.selectProject.setFixedHeight(30)
items = ["Main Projects","Assets"]
self.selectProject.addItems(items)
self.createNewProject = QPushButton()
self.createNewProject.setFixedSize(100,30)
icon = QPixmap('C:/Users/Faithcure/Documents/maya/2016/scripts/projectManager/new.png')
self.createNewProject.setText("New Project")
self.createNewProject.setIcon(icon)
self.dir_label = QLabel()
def select_driectory():
select_path = cm.fileDialog2(fileMode=3, okc = "Select")
self.sceneList.clear()
for path in select_path:
folders = os.listdir(path)
self.sceneList.addItems(folders)
self.dir_label.setText(path)
self.choose_directory = QPushButton()
self.choose_directory.setText("Directory")
self.choose_directory.setIcon(icon)
self.choose_directory.setFixedSize(100,30)
self.choose_directory.clicked.connect(select_driectory)
def new_project_window():
import newProject
reload(newProject)
ui = newProject.start()
ui.exec_()
def selected_item_path():
pass
self.createNewProject.clicked.connect(new_project_window)
This is Qlistwidget from selected directory items.
self.sceneLabel = QLabel()
self.sceneLabel.setText("Episodes: ")
self.sceneList = QListWidget()
self.sceneList.itemClicked.connect(selected_item_path)
And this is my second list widget (i want to list under the selected directories folder from scneneList Widget...
self.shotLabel = QLabel("Shots: ")
self.shotList = QListWidget()
self.topLayout = QHBoxLayout()
self.topLayout.addWidget(self.selectProject)
self.topLayout.addWidget(self.createNewProject)
self.topLayout.addWidget(self.choose_directory)
self.secondLayout = QVBoxLayout()
self.thirdLayout = QHBoxLayout()
self.thirdLayout.addWidget(self.sceneList)
self.thirdLayout.addWidget(self.shotList)
self.labels = QHBoxLayout()
self.labels.addWidget(self.sceneLabel)
self.labels.addWidget(self.shotLabel)
self.saveSceneVer = QPushButton()
self.saveSceneVer.setText("Save New Version")
self.saveSceneVer.setFixedHeight(30)
self.saveSceneVer.setStyleSheet("background-color:#00aff0")
self.openTrack = QPushButton()
self.openTrack.setText("Open Track")
self.openTrack.setFixedHeight(30)
self.openTrack.setStyleSheet("background-color:#ff5a80")
self.opButtons = QHBoxLayout()
self.opButtons.addWidget(self.saveSceneVer)
self.opButtons.addWidget(self.openTrack)
#Tab section for a lat of operations!
self.operation_tabs = QTabWidget()
tabWidget_Animation = QWidget()
optimize_for_animator_scene = QPushButton()
optimize_for_animator_scene.setText("I am an Animator")
create_render_camera = QPushButton()
create_render_camera.setText("Create Camera")
make_playblast = QPushButton()
make_playblast.setText("Playblast")
tabWidget_Animation_Layout = QGridLayout()
tabWidget_Animation.setLayout(tabWidget_Animation_Layout)
tabWidget_Animation_Layout.addWidget(optimize_for_animator_scene,0,0,0)
tabWidget_Animation_Layout.addWidget(create_render_camera, 0,1,0)
tabWidget_Animation_Layout.addWidget(make_playblast, 0,2,0)
tabWidget_render = QWidget()
tabWidget_td = QWidget()
self.operation_tabs.addTab(tabWidget_Animation,"Animation")
self.operation_tabs.addTab(tabWidget_render,"Render")
self.operation_tabs.addTab(tabWidget_td,"Compositing")
self.masterLayout = QVBoxLayout()
self.masterLayout.addLayout(self.topLayout)
self.masterLayout.addWidget(self.dir_label)
self.masterLayout.addLayout(self.secondLayout)
self.masterLayout.addLayout(self.labels)
self.masterLayout.addLayout(self.thirdLayout)
self.masterLayout.addLayout(self.opButtons)
self.masterLayout.addWidget(self.operation_tabs)
self.setLayout(self.masterLayout)
def start():
ui = projectManagerMainWindow()
ui.setWindowTitle("Project Manager")
ui.setFixedWidth(400)
ui.show(dockable=True, floating=False, area='right')
return ui
Here is script for Autodesk Maya.
I'm trying to use QFromLayout for a PySide panel, but I can't get alignment to work. I would like to have all fields line up vertically. Currently they are not lining up, I guess becuse of the length of the label? I tried with bot setHorizontalAlignment() and to fill the label with white spaces, but no luck.
Edit:
Here's a better example of what's not aligned.
Basically I want all "column 2" to be aligned vertically.
Any ideas? Image of panel
import sys
from PySide.QtGui import *
from PySide.QtCore import *
class MyPanel(QWidget):
def __init__(self, parent = None):
super(MyPanel, self).__init__(parent)
self.grp1 = QGroupBox()
self.form_layout1 = QFormLayout()
self.drop1 = QComboBox()
self.drop1.addItems(["item1", "item2"])
self.form_layout1.addRow('drop', self.drop1)
self.line1 = QLineEdit()
self.form_layout1.addRow("long long line", self.line1)
self.btn1 = QPushButton("button")
self.form_layout1.addRow("", self.btn1)
self.grp2 = QGroupBox()
self.form_layout2 = QFormLayout()
self.check2 = QCheckBox()
self.form_layout2.addRow("checkbox", self.check2)
self.line2 = QLineEdit()
self.form_layout2.addRow("line", self.line2)
# Set layouts.
self.grp1.setLayout(self.form_layout1)
self.grp2.setLayout(self.form_layout2)
# Main layout.
self.layout = QVBoxLayout()
self.layout.addWidget(self.grp1)
self.layout.addWidget(self.grp2)
self.setLayout(self.layout)
# Show
app = QApplication(sys.argv)
panel = MyPanel()
panel.show()
app.exec_()
I'm new guy, and i start with new simple GUI application, I follow some tutorial on the internet and now i have a trouble with QGroupbox (Pysde).
This is my code :
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide import QtGui,QtCore
class Form(QtGui.QWidget):
def __init__(self,parent=None):
super(Form,self).__init__(parent)
self.initUi()
def initUi(self):
self.setGeometry(300, 300, 800, 600)
self.setWindowTitle('Library')
self.setMinimumHeight(600)
self.setMinimumWidth(800)
self.setMaximumHeight(600)
self.setMaximumWidth(1100)
#Groupbox Show Only
gpShowonly = QtGui.QGroupBox("Show only :")
gpShowonly.setGeometry(100,100,200,200)
chbx1 = QtGui.QCheckBox("x1")
chbx2 = QtGui.QCheckBox("x2")
chbx3 = QtGui.QCheckBox("x3")
serverlayout = QtGui.QHBoxLayout()
serverlayout.addWidget(chbx1)
serverlayout.addWidget(chbx2)
serverlayout.addWidget(chbx3)
configLayout = QtGui.QVBoxLayout()
configLayout.addLayout((serverlayout))
gpShowonly.setLayout(configLayout)
mainLayout = QtGui.QVBoxLayout()
mainLayout.addWidget(gpShowonly)
mainLayout.addStretch(1)
self.setLayout(mainLayout)
self.show()
app = QApplication.instance()
if app is None:
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
I want it's smaller but it's always full horizontal , I use setGeometry,resize but nothing happen.
The widgets inside a layout will expand to fill the available space. To prevent this, simply add an expandable spacer to the end of the layout:
serverlayout = QtGui.QHBoxLayout()
serverlayout.addWidget(chbx1)
serverlayout.addWidget(chbx2)
serverlayout.addWidget(chbx3)
serverlayout.addStretch()
If this makes the widgets too small, you could give them a minimum width:
for widget in chbx1, chbx2, chbx3:
widget.setMinimumWidth(100)
I am pretty new at python but currently I am getting some problem here with the part where I am unable to get my stuff fit within the width of the window.
I am trying to set it in such a way that it is:
Eg. Name Button
by the way, I am using Maya to integrate and run my stuff.
If I set it to central, it fits but it is all over the place as I only wanted it to occupy a portion only. So are there any ways for me to fit it nicely into it?
By the way, if its possible, can it be done using my current codings?
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sip
import maya.OpenMayaUI as mui
import os
class MainWindow(QMainWindow):
def __init__(self, parent = None):
QMainWindow.__init__(self,parent)
self.resize(400,800)
self.setWindowTitle("GetShots")
self.pubDock = SetShotInfo()
self.pubDW = QDockWidget(self.tr(""), self)
self.pubDW.setWidget(self.pubDock)
# self.setCentralWidget(self.pubDW)
def getMayaWindow():
ptr = mui.MQtUtil.mainWindow()
return sip.wrapinstance(long(ptr), QObject)
def main():
global app
global form
app = qApp
form = MainWindow(getMayaWindow())
form.show()
class GetShot(QFrame):
def __init__(self, parent = None, display=None):
QFrame.__init__(self, parent)
self.createWidgets()
self.createLayout()
def createWidgets(self):
self.showLabel = QLabel('Show')
self.showName = QLineEdit()
self.showName.setText(str(os.environ['SHOW']))
self.shotLabel = QLabel('Shot Filter')
self.shotName = QLineEdit()
self.showButton = QPushButton('Set Show')
self.showButton.setMaximumWidth(200)
self.shotButton = QPushButton('Filter Shots')
self.shotButton.setMaximumWidth(200)
self.rootLabel = QLabel('Change Root')
self.rootButton = QComboBox()
def createLayout(self):
# Sets the Layout of Show and Shot
setShowLayout = QHBoxLayout()
setShowLayout.addWidget(self.showLabel)
setShowLayout.addWidget(self.showName)
setShowLayout.addWidget(self.showButton)
setShotLayout = QHBoxLayout()
setShotLayout.addWidget(self.shotLabel)
setShotLayout.addWidget(self.shotName)
setShotLayout.addWidget(self.shotButton)
# Sets the Change Root Layout
chgRootLayout = QHBoxLayout()
chgRootLayout.addWidget(self.rootLabel)
chgRootLayout.addWidget(self.rootButton)
mainLayout = QVBoxLayout()
mainLayout.addLayout(setShowLayout)
mainLayout.addLayout(setShotLayout)
mainLayout.addLayout(chgRootLayout)
self.setLayout(mainLayout)
if __name__ == '__main__':
main()
You need to use Layouts, combine vertical and horizontal and play with the size policy of the widgets to fit them as you need.
Here's a quick example:
import sys
from PyQt4 import QtCore, QtGui
class ButtonContainer(QtGui.QWidget):
def __init__(self):
super(ButtonContainer, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry( 150, 150, 650, 350)
btn = QtGui.QPushButton('Name button', self)
btn.setSizePolicy( QtGui.QSizePolicy( QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed ) )
vbox = QtGui.QVBoxLayout()
vbox.addWidget( btn )
self.setLayout(vbox)
self.show()
app = QtGui.QApplication(sys.argv)
ex = ButtonContainer()
sys.exit(app.exec_())
The commenters are right in suggesting QtDesigner, if you'd rather code it yourself at least have a mock up ui file where you can play interactively with the layouts and size policies, it's really helpful.