GTK3, SVG, and Cairo widget gets a gtk-missing-image icon overlay - python-3.x

I have a custom widget that I am porting from Python 2 with GTK2 to Python3 with GTK3. I can get it working just fine using a toy example as shown below:
The button is there to change the widget from green to orange to red. Don't ask more.
However, as soon as I include it in a glade file, get this garbage:
Which has the gtk-missing-image icon superimposed on my widget.
The code is a mess with much inheritance and "clever" things hence why there is no code quoted. Either a hint as to why that could be or better yet, a good example of a SVG widget with the ability to change the colours would be welcome.
There's more!
Here is a toy code. It works fine in my toy program (widget SVG appears) but when I load it via glade, I get a gtk-missing-image instead.
class Lamp(gtk.Image):
"""A new lamp?"""
__gtype_name__ = "Lamp"
off_colour = gobject.property(type=str, default="grey40")
on_colour = gobject.property(type=str, default="#00ff00")
fault_colour = gobject.property(type=str, default="#ff0000")
def __init__(self):
super(Lamp, self).__init__()
self._logger = logging.getLogger(self.__class__.__name__)
self._logger.info("Initialised NEW Lamp.")
dir_path = os.path.dirname(os.path.realpath(__file__))
self.path = os.path.join(dir_path, "Lamp.svg")
self.width = 128
self.height = 128
self.preserve_aspect_ratio = True
self.pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
self.path,
self.width,
self.height,
self.preserve_aspect_ratio)
self.set_from_pixbuf(self.pixbuf)
self.connect("draw", self.on_draw)
def on_draw(self, widget, event):
"""When the "draw" event fires, this is run."""
self._logger.debug("%s called.", inspect.currentframe().f_code.co_name)
# … What does here?

Related

In python3 tkinter, the wigdet frame doesn't show in interface

I use the same format of frame but it doesn't show in the interface, hope someone could tell me the solution, thanks.
class Interface(Frame):
def __init__(self,parent=None):
Frame.__init__(self,parent)
self.master.title("measurement")
self.grid()
# fix the size and parameters of widget
self.master.geometry("700x400+100+50")
self.master.Frame1 = Frame(self,relief=GROOVE,bg='white')
self.master.Frame1.grid(column=1,row=9)
self.can =Canvas(self, bg="ivory", width =200, height =150)
self.master.canvas = Canvas(self.master, width=150, height=120, background='snow')
ligne1=self.master.canvas.create_line(75, 0, 75, 120)
if __name__ == "__main__":
window = Tk()
window.resizable(False, False)
Interface(window).mainloop()
I can't figure out why you have 2 Canvas's, but the problem is that you aren't placing them on their respective parents. I cut out a lot of the code that seemed unnecessary and restructured your code to make it more logical:
class Interface(Frame):
def __init__(self, parent):
self.parent = parent
super().__init__(self.parent)
self.Frame1 = Frame(self, relief=GROOVE)
self.Frame1.grid()
self.canvas = Canvas(self.Frame1, bg="ivory", width=200, height=150)
self.canvas.grid()
self.canvas.create_line(75, 0, 75, 120)
if __name__ == "__main__":
root = Tk()
# Tk configurations are not relevant to
# the Interface and should be done out here
root.title('Measurement')
root.geometry('700x400+100+50')
root.resizable(False, False)
Interface(root).pack()
root.mainloop()
i think I don't really understand your problem, you don't see your frame because you don't have any widget in it, that's all
import tkinter as tk
class Interface(tk.Frame):
def __init__(self,parent=None):
tk.Frame.__init__(self,parent)
self.master.title("measurement")
self.grid(row=0, column=0)
# fix the size and parameters of widget
self.master.geometry("700x400+100+50")
self.master.Frame1 = tk.Frame(self,relief='groove',bg='white')
self.master.Frame1.grid(column=1,row=9)
labelExemple =tk.Label(self.master.Frame1, text="Exemple")
labelExemple.grid(row=0,column=0)
self.can = tk.Canvas(self, bg="ivory", width =200, height =150)
self.master.canvas = tk.Canvas(self.master, width=150, height=120, background='snow')
self.ligne1=self.master.canvas.create_line(75, 0, 75, 120)
if __name__ == "__main__":
window = tk.Tk()
window.resizable(False, False)
Interface(window).mainloop()
PS : use import tkinter as tk instead of from tkinter import *
There are several problems with those few lines of code, almost all having to do with the way you're using grid:
you aren't using the sticky option, so widgets won't expand to fill the space they are given
you aren't setting the weight for any rows or columns, so tkinter doesn't know how to allocate unused space
you aren't using grid or pack to put the canvases inside of frames, so the frames stay their default size of 1x1
The biggest problem is that you're trying to solve all of those problems at once. Layout problems are usually pretty simple to solve as long as you're only trying to solve one problem at a time.
Start by removing all of the widgets from Interface. Then, give that frame a distinctive background color and then try to make it fill the window (assuming that's ultimately what you want it to do). Also, remove the root.resizable(False, False). It's rarely something a user would want (they like to be able to control their windows), plus it makes your job of debugging layout problems harder.
Once you get your instance of Interface to appear, add a single widget and make sure it appears too. Then add the next, and the next, adding one widget at a time and observing how it behaves.

QTreeView not spanning parent width or height

So I am new to QtGui and looking up how to do things, and I found this neat example on QTreeView. When I got it working on my own, I noticed that it didn't fill the space as I'd anticipated:
So I have been searching for answers, and not finding much in either Python or C++ resources. I've been checking the documentation a lot, but still not quite finding what I'm searching for.
So it seems clear that something doesn't have the correct size policy, but I am having a hard time figuring out what. I have so far eliminated a couple of potential candidates:
The QWidget instance holding the QTreeView instance is correctly spanning the layout it is in (the QWidget spans the width of the QGroupBox minus a little for margins).
Since QTreeView's parent widget is the correct dimensions, I figured it's something more local to QTreeView, but when I use the setSizePolicy, none of the policies I've used seem to resolve the issue. Possibly multiple steps I'm unaware of?
The QTreeView's inherited viewport (from QAbstractScrollArea is much smaller than I expect. Calling QTreeView's setViewport() method with a new and empty QWidget only redraws the non-header contents background in gray instead of white, and I suspect that this is close but not where I need to look.
QTreeView has other children (besides viewport)that I am still investigating.
Most of what I have tried I left commented out in my code below.
This is my source code to reproduce:
import sys
from PySide.QtGui import *
class TreeTime(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.main_widget = QWidget()
self.main_layout = QVBoxLayout()
self.main_widget.setLayout(self.main_layout)
self.setCentralWidget(self.main_widget)
self.statusBar()
self.make_tree()
self.show()
def make_tree(self):
# init widgets
self.tgb = QGroupBox("[Tree Group Box Title]")
self.main_layout.addWidget(self.tgb)
tgb_layout = QVBoxLayout()
self.tgb.setLayout(tgb_layout)
tgb_widget = QWidget()
tgb_layout.addWidget(tgb_widget)
debug_btn = QPushButton("DEBUG")
tgb_layout.addWidget(debug_btn)
view = QTreeView(parent=tgb_widget)
# view.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
view.setSelectionBehavior(QAbstractItemView.SelectRows)
model = QStandardItemModel()
model.setHorizontalHeaderLabels(['col1', 'col2', 'col3'])
view.setModel(model)
view.setUniformRowHeights(True)
# populate data
for i in range(10):
parent1 = QStandardItem('Family {}. Some long status text for sp'.format(i))
for j in range(3):
child1 = QStandardItem('Child {}'.format(i*3+j))
child2 = QStandardItem('row: {}, col: {}'.format(i, j+1))
child3 = QStandardItem('row: {}, col: {}'.format(i, j+2))
parent1.appendRow([child1, child2, child3])
model.appendRow(parent1)
# span container columns
view.setFirstColumnSpanned(i, view.rootIndex(), True)
# expand third container
index = model.indexFromItem(parent1)
view.expand(index)
# select last row
selmod = view.selectionModel()
index2 = model.indexFromItem(child3)
selmod.select(index2, QItemSelectionModel.Select|QItemSelectionModel.Rows)
def print_debug_info():
print('')
for child in view.children():
print("child "+repr(child)) #not sure what all these are yet
print('')
print('self.main_widget.frameSize: '+repr(self.main_widget.frameSize()))
print('view.parent().parent().frameSize(): '+repr(view.parent().parent().frameSize())) #group box
# print('self.frameSize: '+repr(self.frameSize()))
print('self.tgb.frameSize: '+repr(self.tgb.frameSize()))
print('view.parent(): '+repr(view.parent()))
print('view.parent().frameSize(): '+repr(view.parent().frameSize()))
# print('view.parent().frameSize(): '+repr(view.parent().frameSize())+" (before)")
# print('view.parent().adjustSize(): '+repr(view.parent().adjustSize()))
# print('view.parent().frameSize(): '+repr(view.parent().frameSize())+" (after)")
print('view.viewport(): '+repr(view.viewport()))
print('view.viewport().frameSize(): '+repr(view.viewport().frameSize()))
# print('view.parent().parent().parent().frameSize(): '+repr(view.parent().parent().parent().frameSize()))
# print('calling setViewport: '+repr(view.setViewport(QWidget())))
# view.adjustSize()
debug_btn.clicked.connect(print_debug_info)
def sayHello(self):
self.statusBar().showMessage("Hello World!")
import time; time.sleep(2)
self.statusBar().showMessage("")
def sayWords(self, words):
self.statusBar().showMessage(words)
if __name__ == '__main__':
app = QApplication([])
tt = TreeTime()
sys.exit(app.exec_())
I am using a Windows 8.1 machine and Python 3.4.3, PySide version 1.2.2 - any help will be much appreciated! (also, please let me know if I left out any important details)
UPDATE (5/19/2015): I tried moving my DEBUG button outside the QGroupBox, and the result was the QTreeView being collapsed into a completely nonlegible size so you couldn't even tell what the object was anymore, so it seems to be minimizing the space used, even when I uncomment the line:
view.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
One friend has suggested this may simply be an issue with windows and not my code, but I don't have anything to back that up.
UPDATE 5/19/2015: I have implemented the advice provided by #titusjan, but I have the same problem/behavior.
You need to remove the redundant tp_widget and add view to tgb_layout:
def make_tree(self):
# init widgets
self.tgb = QGroupBox("[Tree Group Box Title]")
self.main_layout.addWidget(self.tgb)
tgb_layout = QVBoxLayout()
self.tgb.setLayout(tgb_layout)
view = QTreeView()
tgb_layout.addWidget(view)
...
debug_btn = QPushButton("DEBUG")
tgb_layout.addWidget(debug_btn)
Note that when you add widgets to a layout, they will be automatically re-parented to the parent of the layout (whenever it gets one), so it's not really necessary set one in the constructor.
Also note that this:
tgb_layout = QVBoxLayout(self.tgb)
is exactly equivalent to this:
tgb_layout = QVBoxLayout()
self.tgb.setLayout(tgb_layout)
because the layout will always be re-parented to the widget it's set on.
You must use the setLayout method to link the layout to the widget. So change...
self.main_layout = QVBoxLayout(self.main_widget)
into
self.main_layout = QVBoxLayout()
self.main_widget.setLayout(self.main_layout)
Similar for the tgb_view layout (which I would rename to tgb_layout for clarity).
Finally you forgot to add the tree view to this layout, so add:
tgb_view.addWidget(view)
I've put all the relevant modified code below for convenience.
def initUI(self):
self.main_widget = QWidget()
self.main_layout = QVBoxLayout()
self.main_widget.setLayout(self.main_layout)
self.setCentralWidget(self.main_widget)
self.statusBar()
self.make_tree()
self.show()
def make_tree(self):
# init widgets
self.tgb = QGroupBox("[Tree Group Box Title]")
self.main_layout.addWidget(self.tgb)
tgb_view = QVBoxLayout()
self.tgb.setLayout(tgb_view)
tgb_widget = QWidget()
tgb_view.addWidget(tgb_widget)
debug_btn = QPushButton("DEBUG")
tgb_view.addWidget(debug_btn)
view = QTreeView(parent=tgb_widget)
tgb_view.addWidget(view)
...
The size policy stuff is not necessary, the defaults are fine.

Is there a way to put the text of a QCheckBox above the icon?

I have a gridlayout that holds a bunch of check boxes. I wanted to add an image to the check boxes as well as some text. The problem I am having is that the layout of a check box is left to right (check box, icon, text).
Is there a way to put the text above the icon? Not sure if using a style sheet would work for this or not or even how that would look.
Thank you.
Answer : In PyQt4. No, your can't do it.
Why ? I read source code of QCheckBox Qt4 (C++) here and here. I saw it use default QStyleOptionButton to show check box, text and icon. It's use drawControl to draw all element in QStyleOptionButton by specified config in QStyleOptionButton. Also it have LayoutDirection. And layout direction in QStyleOptionButton. I don't know in Qt4 C++ and inheritance it and swap direction icon. But in PyQt4, It's impossible to do it.
Another way ? : Yes, It have another way to solve but not directly. Your just create your own widget just like QCheckBox and disable icon in QCheckBox and make your own QLabel ot show your icon and set it with same QLayout.
Example;
import sys
from PyQt4 import QtGui, QtCore
class QCustomCheckBox (QtGui.QWidget):
stateChanged = QtCore.pyqtSignal(int)
def __init__ (self, text, parentQWidget = None):
super(QCustomCheckBox, self).__init__(parentQWidget)
self.customQCheckBox = QtGui.QCheckBox(text)
self.iconQLabel = QtGui.QLabel()
allQHBoxLayout = QtGui.QHBoxLayout()
allQHBoxLayout.addWidget(self.customQCheckBox)
allQHBoxLayout.addWidget(self.iconQLabel)
allQHBoxLayout.addStretch(1)
self.setLayout(allQHBoxLayout)
self.customQCheckBox.stateChanged.connect(self.stateChanged.emit)
def setPixmap (self, newQPixmap, width = 48, height = 48):
self.iconQLabel.setPixmap(newQPixmap.scaled(width, height, QtCore.Qt.KeepAspectRatio))
def pixmap (self):
return self.iconQLabel.pixmap()
class QCustomWidget (QtGui.QWidget):
def __init__ (self, parent = None):
super(QCustomWidget, self).__init__(parent)
allQVBoxLayout = QtGui.QVBoxLayout()
firstQCustomCheckBox = QCustomCheckBox('First Check Box')
firstQCustomCheckBox.setPixmap(QtGui.QPixmap('1.jpg'))
allQVBoxLayout.addWidget(firstQCustomCheckBox)
secondQCustomCheckBox = QCustomCheckBox('Second Check Box')
secondQCustomCheckBox.setPixmap(QtGui.QPixmap('2.jpg'))
allQVBoxLayout.addWidget(secondQCustomCheckBox)
self.setLayout(allQVBoxLayout)
if __name__ == '__main__':
myQApplication = QtGui.QApplication(sys.argv)
myQCustomWidget = QCustomWidget()
myQCustomWidget.show()
sys.exit(myQApplication.exec_())

Qt formlayout not expanding qplaintextedit vertically

I'm confused why a QPlainTextEdit widget will not resize vertically when added to a QFormLayout. In the code below the text field correctly scales up horizontally, but does not scale up vertically.
Can anyone explain this behavior and offer a solution? I've tried all the tricks I know to no avail.
from PyQt4 import QtGui
class Diag(QtGui.QDialog):
def __init__(self, parent, *args, **kwargs):
QtGui.QDialog.__init__(self, parent)
layout = QtGui.QFormLayout(self)
widg = QtGui.QPlainTextEdit(self)
layout.addRow('Entry', widg)
if __name__ == '__main__': #pragma: no cover
app = QtGui.QApplication([])
window = Diag(None)
window.show()
app.exec_()
Here is an example of the QPlainTextEdit widget not resizing vertically:
This is on Windows 7 using PyQt 4.5.2 and Python 32-bit 2.6.
Thanks.
It seems that, by default, a QFormLayout will only resize the height of its fields according to their sizeHint.
To change this behaviour, adjust the vertical stretch as appropriate:
policy = widg.sizePolicy()
policy.setVerticalStretch(1)
widg.setSizePolicy(policy)
You should set the object in the last row of formlayout (see QPlainTextEdit), its vertical Stretch factor should not be 0.
This works for me:
it is small at the time of calculating the initial size of the dialog widget and can grow with the dialog once it is already visible
class q2text(QTextEdit):
def showEvent(self, ev):
self.updateGeometry()
return super().showEvent(ev)
def sizeHint(self):
if self.isVisible():
return QSize(99999, 99999)
else:
return super().sizeHint()

PYQT QSplitter issue

I use QSplitter and I found out that the minumum width of a widget in
the splitter is 32 pixels (and 23 pixels in height). Does anybody body knows how
to change this default. In other words, you can't drag the splitter so that one of the
widgets (assume that there are 2 widgets in the spllitter) in the spllitter will be less
than 32 pixels in width.
The code:
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.resize(400,400)
m = QtGui.QSplitter(self)
m.resize(200, 100)
x = QtGui.QPushButton(m)
x.setGeometry(0, 0, 100, 100)
y = QtGui.QPushButton(m)
y.setGeometry(0, 100, 100, 100)
m.setSizes([20, 180])
# this will show you that the width of x is 32 (it should be 20!)
print x.width()
Note: I'm using Python 3.6.2 and PyQt5, though the logic in the example stays the same and can be understood even if you're using other versions of Python and PyQt.
Look at what is said here:
If you specify a size of 0, the widget will be invisible. The size policies of the widgets are preserved. That is, a value smaller than the minimal size hint of the respective widget will be replaced by the value of the hint.
One of the options to solve your problem is to call x.setMinimumWidth() with a small value, like:
x.setMinimumWidth(1)
However, if you'll try it yourself, you'll see that
it is a dirty hack as it actually leaves the widget here, just makes it very narrow and
though now you can drag the splitter, the initial width of the widget is still "32" instead of "20".
x.setMinimumWidth(0)
also doesn't work as expected: its minimal width is actually zero by default (as this widget has no contents, I guess), but it doesn't help you to make splitter item less than 32 pixels wide unless you collapse it.
By the way, set
m.setCollapsible(0, False)
m.setCollapsible(1, False)
if you want splitter to stop collapsing its two children widgets. More details here.
The solution I've found is to overload sizeHint() method of the widget you want to include into the splitter, as in example below (look at the ButtonWrapper class and what is output like now).
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#Python 3.6.2 and PyQt5 are used in this example
from PyQt5.QtWidgets import (
QPushButton,
QSplitter,
QWidget,
QApplication,
)
import sys
class ButtonWrapper(QPushButton):
def sizeHint(self):
return self.minimumSize()
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.resize(400, 400)
m = QSplitter(self)
m.resize(200, 100)
x = ButtonWrapper(self)
x.setGeometry(0, 0, 100, 100)
y = QPushButton(self)
y.setGeometry(0, 100, 100, 100)
m.addWidget(x)
m.addWidget(y)
m.setSizes([20, 180])
#Now it really shows "20" as expected
print(x.width())
#minimumWidth() is zero by default for empty QPushButton
print(x.minimumWidth())
#Result of our overloaded sizeHint() method
print(x.sizeHint().width())
print(x.minimumSizeHint().width())
self.setWindowTitle('Example')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
I'm not sure if this is the right way to do stuff, but I've spent lots of time trying to solve my own problem connected to this, and haven't seen anything satisfying yet so far. I'll really appreciate it if someone knows a better actually working & clear workaround.

Resources