This question already has an answer here:
PyQt5 label cut off
(1 answer)
Closed last year.
I have this Lable in my PyQt GUI: self.labelDirectory.setText("Insert directories name using a semicolumn (;) to separate them").
But in the actual application it's cutted and only show the first part of the text photo here
I tried using a new line (\n) but it doesn't do much with new line
full code:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(0, 0, 800, 800)
self.setWindowTitle("BocaWare")
self.initUi()
def initUi(self):
#data inizio
self.labelFirstDate = QtWidgets.QLabel(self)
self.labelFirstDate.setText("First Date")
self.labelFirstDate.move(20, 0)
self.firstDate = QtWidgets.QDateEdit(self)
self.firstDate.setGeometry(QtCore.QRect(10, 30, 100, 30))
#data fine
self.labelEndDate = QtWidgets.QLabel(self)
self.labelEndDate.setText("End Date")
self.labelEndDate.move(20, 60)
self.endDate = QtWidgets.QDateEdit(self)
self.endDate.setGeometry(QtCore.QRect(10, 85, 100, 30))
#casella testo per nome directory
self.labelDirectory = QtWidgets.QLabel(self)
#self.labelDirectory.setText("Inserire i nomi delle directory separati l'un l'altro con un punto e virgola (;)")
self.labelDirectory.setText("Insert directories \n name using a semicolumn (;) to separate them")
self.labelDirectory.move(20, 120)
self.directory = QtWidgets.QLineEdit(self)
self.directory.setGeometry(QtCore.QRect(10, 170, 400, 30))
#bottone invio
self.b1 = QtWidgets.QPushButton(self)
self.b1.setGeometry(QtCore.QRect(10, 200, 100, 30))
self.b1.setText("Submit")
self.b1.clicked.connect(self.submitClick)
def submitClick(self):
print(self.firstDate.date().toString("yyyy-MM-dd"))
print(self.endDate.date().toString("yyyy-MM-dd"))
print(self.directory.text())
directories = self.directory.text().split(";")
for directory in directories:
print(directory)
def window():
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
window()
Using some kind of layout is going to be helpful rather than manually sizing and positioning widgets. It looks like your label is just getting clipped based on its size, but rather than fixing that, maybe try a simple QFormLayout. You could also use a combinations of QVBoxLayout, QHBoxLayout, and QSpacer to achieve something similar.
form layout
from PyQt5 import QtWidgets
import sys
class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(0, 0, 800, 800)
self.setWindowTitle("BocaWare")
self._init_ui()
def _init_ui(self):
self.central_widget = QtWidgets.QWidget(self)
self.setCentralWidget(self.central_widget)
self.layout = QtWidgets.QFormLayout(self.central_widget)
# data inizio
self.labelFirstDate = QtWidgets.QLabel(self)
self.labelFirstDate.setText("First Date")
self.firstDate = QtWidgets.QDateEdit(self)
# data fine
self.labelEndDate = QtWidgets.QLabel(self)
self.labelEndDate.setText("End Date")
self.endDate = QtWidgets.QDateEdit(self)
# casella testo per nome directory
self.labelDirectory = QtWidgets.QLabel(self)
# self.labelDirectory.setText("Inserire i nomi delle directory separati l'un l'altro con un punto e virgola (;)")
self.labelDirectory.setText("Insert directories name using a semicolumn (;) to separate them")
self.directory = QtWidgets.QLineEdit(self)
# bottone invio
self.b1 = QtWidgets.QPushButton(self)
self.b1.setText("Submit")
self.b1.clicked.connect(self.submit_click)
self.layout.addRow(self.labelFirstDate)
self.layout.addRow(self.firstDate)
self.layout.addRow(self.labelEndDate)
self.layout.addRow(self.endDate)
self.layout.addRow(self.labelDirectory)
self.layout.addRow(self.directory)
self.layout.addRow(self.b1)
def submit_click(self):
print(self.firstDate.date().toString("yyyy-MM-dd"))
print(self.endDate.date().toString("yyyy-MM-dd"))
print(self.directory.text())
directories = self.directory.text().split(";")
for directory in directories:
print(directory)
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
Related
I am trying to create a scroll area with a vertical layout using pyqt5, and I am putting inside some labels. I know that even if it is a vertical layout it is supposed to scroll horizontally if the text does not fit. But no matter what I try it does not let me scroll.
This is the code I am using:
class window(QMainWindow):
def __init__(self):
super(window, self).__init__()
self.setGeometry(300, 300, 803, 520)
self.init_ui()
def init_ui(self):
self.teacher_box = QScrollArea(self)
self.teacher_box.setGeometry(360, 10, 420, 181)
self.teacher_box.setWidgetResizable(True)
self.teacher_box.setObjectName("teacher_box")
self.teacher_box_widget = QWidget()
self.teacher_box_widget.setGeometry(QtCore.QRect(0, 0, 420, 181))
self.teacher_box_widget.setObjectName("teacher_box_widget")
self.verticalLayout = QVBoxLayout(self.teacher_box_widget)
self.verticalLayout.setObjectName("verticalLayout")
self.teacher_box.setWidget(self.teacher_box_widget)
self.teacher_label = QtWidgets.QLabel(self.teacher_box_widget)
self.teacher_label.setText("This is a new text label that i created using pyqt5's qscrollarea and now the label is going to get outside the boundaries")
self.teacher_label.adjustSize()
self.teacher_label.move(10, 10)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = window()
win.show()
sys.exit(app.exec_())
Here is how it should look:
Here is how it looks:
I hope my question is clear
Since many questions wonder how to use a QScrollArea that many widgets have, I will take the license to explain the various forms in detail and use them as a canonical answer for future readers.
QScrollArea only allows placing a container widget so the other widgets must be placed as children of the container widget.
And to place the widgets as children of another there are 2 possibilities:
1. Use a QLayout:
QLayouts allow you to automate the geometry of the widgets based on the QSizePolicy, strecth, sizeHint, etc. So in that case it's simple: Just set the widgetResizable property to True.
import sys
from PyQt5 import QtWidgets
class Window(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
scroll_area = QtWidgets.QScrollArea(central_widget)
scroll_area.setGeometry(360, 10, 420, 180)
scroll_area.setWidgetResizable(True)
container = QtWidgets.QWidget()
scroll_area.setWidget(container)
# Set widgets via layout
lay = QtWidgets.QVBoxLayout(container)
lay.setContentsMargins(10, 10, 0, 0)
for letter in "ABCDE":
text = letter * 100
label = QtWidgets.QLabel(text)
lay.addWidget(label)
lay.addStretch()
self.setGeometry(300, 300, 803, 520)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())
2. Set the widgets directly without layouts:
In this case you must calculate the minimum geometry that contains the internal widgets and set the size in the container, also you must set the widgetResizable property to False:
import sys
from PyQt5 import QtCore, QtWidgets
class Window(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
scroll_area = QtWidgets.QScrollArea(central_widget)
scroll_area.setGeometry(360, 10, 420, 180)
scroll_area.setWidgetResizable(False)
container = QtWidgets.QWidget()
scroll_area.setWidget(container)
# calculate geometry
geometry = QtCore.QRect(10, 10, 0, 0)
for letter in "ABCDE":
text = letter * 100
label = QtWidgets.QLabel(text, container)
label.adjustSize()
label.move(geometry.bottomLeft())
geometry |= label.geometry()
geometry.setTopLeft(QtCore.QPoint(0, 0))
container.resize(geometry.size())
self.setGeometry(300, 300, 803, 520)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())
In PyQt5, how do I move to another label when clicking a button from a label on a StackedWidget?
When you click pushButton_3, I want the After screen(After label) to appear.
What code should I use to display the After screen(After label)?
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_3.clicked.connect(self.click_next)
def click_next(self):
pass
## I want to move to After label
Full Code :
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Before(QWidget): ## Before label
def __init__(self):
super(Before, self).__init__()
self.label = QtWidgets.QLabel(self)
self.label.setGeometry(QtCore.QRect(240, 210, 301, 81))
font = QtGui.QFont()
font.setPointSize(30)
self.label.setFont(font)
self.label.setText("Before 화면") # 화면 == screen
self.label.setObjectName("label")
self.pushButton_3 = QtWidgets.QPushButton(self)
self.pushButton_3.setGeometry(QtCore.QRect(590, 220, 141, 61))
font = QtGui.QFont()
font.setPointSize(20)
self.pushButton_3.setText("NEXT")
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_3.clicked.connect(self.click_next)
def click_next(self):
pass
## I want to move to After label
class After(QWidget): ## After label
def __init__(self):
super(After, self).__init__()
self.label_2 = QtWidgets.QLabel(self)
self.label_2.setGeometry(QtCore.QRect(240, 210, 301, 81))
font = QtGui.QFont()
font.setPointSize(30)
self.label_2.setFont(font)
self.label_2.setText("After 화면") # 화면 == screen
self.label_2.setObjectName("label_2")
self.pushButton_4 = QtWidgets.QPushButton(self)
self.pushButton_4.setGeometry(QtCore.QRect(30, 220, 141, 61))
font = QtGui.QFont()
font.setPointSize(20)
self.pushButton_4.setText("BEFORE")
self.pushButton_4.setFont(font)
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_4.clicked.connect(self.click_before)
def click_before(self):
pass
## I want to move to Before label
class Ui_StackedWidget(QWidget):
def __init__(self):
QWidget.__init__(self, flags=Qt.Widget)
self.stk_w = QStackedWidget(self)
self.setupUi()
def setupUi(self):
self.setWindowTitle("Example3-02")
self.resize(800, 600)
widget_laytout = QBoxLayout(QBoxLayout.LeftToRight)
self.stk_w.addWidget(Before())
self.stk_w.addWidget(After())
widget_laytout.addWidget(self.stk_w)
self.setLayout(widget_laytout)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Ui_StackedWidget()
form.show()
exit(app.exec_())
This is the Before screen.
This is the After screen.
You can connect those buttons from the main window to a function that switches the pages of the stacked widget. In order to do so, you should keep a reference to those widgets, instead of creating them within addWidget().
class Ui_StackedWidget(QWidget):
def __init__(self):
QWidget.__init__(self, flags=Qt.Widget)
self.stk_w = QStackedWidget(self)
self.setupUi()
def setupUi(self):
self.setWindowTitle("Example3-02")
self.resize(800, 600)
widget_laytout = QBoxLayout(QBoxLayout.LeftToRight)
self.before = Before()
self.stk_w.addWidget(self.before)
self.after = After()
self.stk_w.addWidget(self.after)
widget_laytout.addWidget(self.stk_w)
self.setLayout(widget_laytout)
self.before.pushButton_3.clicked.connect(self.goToAfter)
self.after.pushButton_4.clicked.connect(self.goToBefore)
def goToAfter(self):
self.stk_w.setCurrentWidget(self.after)
def goToBefore(self):
self.stk_w.setCurrentWidget(self.before)
Note: there's no need to add the flags argument to the __init__, all QWidgets already have that flag set. Also, if you're not dealing with right-to-left languages, you can just use create a QHBoxLayout() instead of a QBoxLayout(QBoxLayout.LeftToRight): while the result is the same, it's more readable and the preferred convention.
Some alternative way to other answer if you're planning to use more than 2 widgets:
from PySide2.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout, QLabel, QStackedWidget
import sys
# Using PySide2 as an alternative to PyQt5 - Only difference would be import route.
class Base(QWidget):
def __init__(self, label):
super().__init__()
self.label = QLabel(label)
self.previous_button = QPushButton("Next")
self.layout = QVBoxLayout()
self.layout.addWidget(self.label)
self.layout.addWidget(self.previous_button)
self.setLayout(self.layout)
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.widgets = [Base(f"{i}번 화면") for i in range(3)]
self.stack = QStackedWidget()
for widget in self.widgets:
widget.previous_button.released.connect(self.onSignal)
self.stack.addWidget(widget)
self.layout = QVBoxLayout()
self.layout.addWidget(self.stack)
self.setLayout(self.layout)
def onSignal(self):
current_idx = self.stack.currentIndex()
idx_next = 0 if current_idx == self.stack.count() - 1 else current_idx + 1
self.stack.setCurrentIndex(idx_next)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
You can cycle thru multiple instances in this case, or add reverse button to cycle both way.
I am creating an application where I have a main window whit a label and then a docked widget that is in another file. I want to change the main windows label from a button at the docked widget. I try to import the main window file but then I can not access to the label. And I also tried to call a function in the main windows that changes the label but then the label does not change.
Here is the code:
main_window.py:
import results_window
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.define_main_windows()
self.create_dock_widgets()
def define_main_windows(self):
# Define de Main window properties
self.setMinimumSize(QSize(300, 100))
self.setWindowTitle("Python SkyLibris")
self.setWindowIcon(QtGui.QIcon("skylibris_icon.png"))
self.setStyleSheet("QMainWindow {background: 'white';}")
self.top = 50
self.left = 0
self.width = 1300
self.height = 400
self.setGeometry(self.left, self.top, self.width, self.height)
self.result = QLabel("result:")
self.setCentralWidget(self.result)
def create_dock_widgets(self):
# Create dock widgets
self.results_window = results_window.results_window()
self.resultsWindowDock = QDockWidget("Results Viewer", self)
self.resultsWindowDock.setWidget(self.results_window )
self.resultsWindowDock.setFloating(False)
self.resultsWindowDock.setVisible(True)
self.addDockWidget(Qt.LeftDockWidgetArea, self.resultsWindowDock)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
results_window.py:
import main_window
class results_window(QWidget):
def __init__(self):
super(results_window, self).__init__()
print("init")
self.label = QLabel()
self.value = QLineEdit()
self.bt = QPushButton("Click")
self.bt.clicked.connect(self.clickMethod)
self.main_layout = QVBoxLayout()
self.main_layout.addWidget(self.label)
self.main_layout.addWidget(self.value)
self.main_layout.addWidget(self.bt)
self.setLayout(self.main_layout)
def clickMethod(self):
print(self.value.text())
text = self.value.text()
main_window.result.setText(text)
You are using the wrong tools, for example your code has a circular import that causes your application to close since it is equivalent to a while True.
In Qt, signals and slots are used to share data asynchronously, as well as contributing to the fact that there is no coupling between classes. In your case, Results_Window must have a signal that transmits that information to the MainWindow, this signal must be emit within clickMethod.
results_window.py
from PyQt5 import QtCore, QtWidgets
class Results_Window(QtWidgets.QWidget):
resultChanged = QtCore.pyqtSignal(str)
def __init__(self):
super(Results_Window, self).__init__()
print("init")
self.label = QtWidgets.QLabel()
self.value = QtWidgets.QLineEdit()
self.bt = QtWidgets.QPushButton("Click")
self.bt.clicked.connect(self.clickMethod)
main_layout = QtWidgets.QVBoxLayout(self)
main_layout.addWidget(self.label)
main_layout.addWidget(self.value)
main_layout.addWidget(self.bt)
#QtCore.pyqtSlot()
def clickMethod(self):
text = self.value.text()
self.resultChanged.emit(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
w = Results_Window()
w.show()
sys.exit(app.exec_())
main_window.py
from PyQt5 import QtCore, QtGui, QtWidgets
import results_window
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.define_main_windows()
self.create_dock_widgets()
def define_main_windows(self):
self.setMinimumSize(QtCore.QSize(300, 100))
self.setWindowTitle("Python SkyLibris")
self.setWindowIcon(QtGui.QIcon("skylibris_icon.png"))
self.setStyleSheet("QMainWindow {background: 'white';}")
top, left, width, height = 50, 0, 1300, 400
self.setGeometry(left, top, width, height)
self.result = QtWidgets.QLabel("result:")
self.setCentralWidget(self.result)
def create_dock_widgets(self):
self.results_window = results_window.Results_Window()
self.results_window.resultChanged.connect(self.result.setText)
self.resultsWindowDock = QtWidgets.QDockWidget("Results Viewer", self)
self.resultsWindowDock.setWidget(self.results_window )
self.resultsWindowDock.setFloating(False)
self.resultsWindowDock.setVisible(True)
self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.resultsWindowDock)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
I had similar problem in PyQT5 where I was unable to access and set the local variables. After a lot of struggle I found writing to file and reading from file as the best solution. Simply write the desired output to file and access the same info from other file. Works great!
this is my very first post on stackoverflow. To this day stackoverflow has been a very huge help for me improving my python skills.
But I'm having this problem, that PyQt adds only one widget to the centralWidget instead of the 9x9 matrix I need for Sudoku. In another version - which worked - I used two classes to create the MainWindow and the Widgets seperatly. But now I want to achieve it in only one class.
link1: how it looks
link2: how it should look
import sys
from selenium import webdriver
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class mainwindow(QMainWindow, QWidget):
def __init__(self, parent = None):
super(mainwindow, self).__init__(parent = parent)
self.title = 'SUDOKU SOLVER'
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
# add menubar
menubar = self.menuBar()
# add drop down items
exitAct = QAction('&Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit Application')
exitAct.triggered.connect(qApp.quit)
newAct = QAction('New', self)
newAct.setShortcut('Ctrl+N')
newAct.setStatusTip('New Sudoku')
newAct.triggered.connect(GameLogic.clearFields)
rulesAct = QAction('Rules', self)
rulesAct.setShortcut('Ctrl+R')
rulesAct.setStatusTip('Sudoku Rules')
rulesAct.triggered.connect(GameLogic.sudokuRules)
# add menubar entries
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(newAct)
fileMenu.addAction(exitAct)
helpMenu = menubar.addMenu('&Help')
helpMenu.addAction(rulesAct)
# call gridlayout function
l = self.gridLayout()
self.setCentralWidget(l)
self.show()
def gridLayout(self):
layout = QGridLayout()
solve = QPushButton('Solve', self)
solve.clicked.connect(GameLogic.solveSudoku)
solve.setFixedSize(60, 30)
mainwindow.fields = {}
# validate user input
onlyInt = QIntValidator(1, 9, self)
# this is the part that doesnt work...
for x in range(9):
for y in range(9):
# keep a reference to the buttons
mainwindow.fields[(x, y)] = QLineEdit(self)
mainwindow.fields[(x, y)].setMaxLength(1)
mainwindow.fields[(x, y)].setValidator(onlyInt)
mainwindow.fields[(x, y)].setFixedSize(60, 60)
mainwindow.fields[(x, y)].setFont(QFont('Sans Serif', 20))
mainwindow.fields[(x, y)].setAlignment(Qt.AlignCenter)
# add to the layout
layout.addWidget(mainwindow.fields[(x, y)], x, y)
layout.addWidget(solve, 10, 4)
self.setLayout(layout)
class GameLogic():
def clearFields(self):
for i in range(9):
for j in range(9):
mainwindow.fields[(i, j)].clear()
def sudokuRules(self):
pass
def solveSudoku(self):
pass
def main():
app = QApplication(sys.argv)
ex = mainwindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Without running it myself, or having used pyQt before,
setCentralWidget() needs a widget as a parameter.
You don't seem to return anything from gridLayout().
You might need to create a widget.
Use its layout to add all your things.
And then assign this widget via setCentralWidget().
P.S.: Maybe it works if you just remove the l = *; and setCentralWidget(); lines and just call gridLayout() (perhaps rename it to createGridLayout())
You have a few basic mistakes but nothing more.
Firstly, there's no need for mainwindow to inherit from both QMainWindow and QWidget, so
class mainwindow(QMainWindow, QWidget):
becomes...
class mainwindow(QMainWindow):
Secondly, in mainwindow::gridLayout you place your grid of controls in a layout and then do...
self.setLayout(layout)
That should almost certainly result in a warning along the lines of...
QWidget::setLayout: Attempting to set QLayout "" on mainwindow "",
which already has a layout
Instead, create a new QWidget and use it as the container for your new layout then return that QWidget from gridLayout. So...
self.setLayout(layout)
becomes...
w = QWidget()
w.setLayout(layout)
return(w)
Complete Code:
import sys
from selenium import webdriver
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class mainwindow(QMainWindow):
def __init__(self, parent = None):
super(mainwindow, self).__init__(parent = parent)
self.title = 'SUDOKU SOLVER'
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
# add menubar
menubar = self.menuBar()
# add drop down items
exitAct = QAction('&Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit Application')
exitAct.triggered.connect(qApp.quit)
newAct = QAction('New', self)
newAct.setShortcut('Ctrl+N')
newAct.setStatusTip('New Sudoku')
newAct.triggered.connect(GameLogic.clearFields)
rulesAct = QAction('Rules', self)
rulesAct.setShortcut('Ctrl+R')
rulesAct.setStatusTip('Sudoku Rules')
rulesAct.triggered.connect(GameLogic.sudokuRules)
# add menubar entries
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(newAct)
fileMenu.addAction(exitAct)
helpMenu = menubar.addMenu('&Help')
helpMenu.addAction(rulesAct)
# call gridlayout function
l = self.gridLayout()
self.setCentralWidget(l)
self.show()
def gridLayout(self):
layout = QGridLayout()
solve = QPushButton('Solve', self)
solve.clicked.connect(GameLogic.solveSudoku)
solve.setFixedSize(60, 30)
mainwindow.fields = {}
# validate user input
onlyInt = QIntValidator(1, 9, self)
# this is the part that doesnt work...
for x in range(9):
for y in range(9):
# keep a reference to the buttons
mainwindow.fields[(x, y)] = QLineEdit(self)
mainwindow.fields[(x, y)].setMaxLength(1)
mainwindow.fields[(x, y)].setValidator(onlyInt)
mainwindow.fields[(x, y)].setFixedSize(60, 60)
mainwindow.fields[(x, y)].setFont(QFont('Sans Serif', 20))
mainwindow.fields[(x, y)].setAlignment(Qt.AlignCenter)
# add to the layout
layout.addWidget(mainwindow.fields[(x, y)], x, y)
layout.addWidget(solve, 10, 4)
w = QWidget()
w.setLayout(layout)
return(w)
class GameLogic():
def clearFields(self):
for i in range(9):
for j in range(9):
mainwindow.fields[(i, j)].clear()
def sudokuRules(self):
pass
def solveSudoku(self):
pass
def main():
app = QApplication(sys.argv)
ex = mainwindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I'm trying to create a Application form by PySide, and i follow some tutorial , but i have a problem that is a space between QLabel and QCombobox.
This is my code
import sys
from PySide import QtCore, QtGui
from PySide.QtGui import *
from PySide.QtCore import *
class Window(QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.setMinimumHeight(660)
self.setMinimumWidth(700)
self.setMaximumHeight(660)
self.setMaximumWidth(900)
grid = QtGui.QGridLayout()
grid.addWidget(self.First(), 0,0,2,0)
self.setLayout(grid)
self.setWindowTitle("Library")
self.resize(700, 660)
def First(self):
groupBox = QtGui.QFrame()
groupBox.setMaximumWidth(230)
groupBox.setMaximumHeight(700)
lbFile = QtGui.QLabel("File :",self)
lbFolders = QtGui.QLabel("Folders :",self)
cbFile = QtGui.QComboBox(self)
cbFile.addItem("File 1")
cbFile.addItem("File 2")
lvFolders = QtGui.QListView(self)
lvFolders.setMaximumWidth(220)
lvFolders.setMaximumHeight(500)
vbox = QtGui.QGridLayout()
vbox.addWidget(lbFile,0,0)
vbox.addWidget(cbFile,0,1)
vbox.addWidget(lbFolders,2,0)
vbox.addWidget(lvFolders,3,0,1,2)
groupBox.setLayout(vbox)
return groupBox
app = QApplication.instance()
if app is None:
app = QApplication(sys.argv)
clock = Window()
clock.show()
app.exec_()
and this is my Form :
I dont know how to delete the space between QLabel "File" and QCombobox "File 1"
Adjust the stretch factor for the second column of the layout, and possibly also make the folders label span both columns:
vbox = QtGui.QGridLayout()
vbox.addWidget(lbFile,0,0)
vbox.addWidget(cbFile,0,1)
vbox.addWidget(lbFolders,2,0,1,2)
vbox.addWidget(lvFolders,3,0,1,2)
vbox.setColumnStretch(1, 1)