I want a BitMapButton Class with some toggle functionality:
first press ON, OFF next press etc.
This should be visible in the color of the Button.
I tried the following (needs some tiny 'off.gif', 'on.gif' files):
import wx
class BitmapToggleButton(wx.BitmapButton):
"""make a Button, inherits wx.BitmapButton, add a toggleState"""
#----------------------------------------------------------------------
def __init__(self, parent, filename1, filename2):
"""Constructor"""
self.state = False
self.image1 = wx.Image(filename1, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.image2 = wx.Image(filename2, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
#wx.BitmapButton.__init__(self, id=-1, bitmap=self.image2, pos=(10, 20), size = (300, 400))
self.image1 = wx.Image(filename1, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.image2 = wx.Image(filename2, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.button = wx.BitmapButton(parent, id=-1, bitmap=self.image1, pos=(10, 20), size = (self.image1.GetWidth()+5, self.image1.GetHeight()+5))
def OnClick(self, event):
"""does the toggling"""
if self.state:
self.state = False
self.button.SetBitmapLabel(self.image2)
else:
self.state = True
self.button.SetBitmapLabel(self.image1)
self.Refresh()
class MyFrame(wx.Frame):
"""make a frame, inherits wx.Frame, add a panel and button"""
def __init__(self):
# create a frame, no parent, default to wxID_ANY
wx.Frame.__init__(self, None, wx.ID_ANY, 'wxBitmapButton', pos=(300, 150), size=(300, 350))
#panel to display button
self.panel1 = wx.Panel(self, -1)
#test it
self.button = BitmapToggleButton(self, 'off.gif', 'on.gif')
self.Bind(wx.EVT_BUTTON, self.button.OnClick, self.button)
# show the frame
self.Show(True)
application = wx.PySimpleApp()
# call class MyFrame
f = MyFrame()
# start the event loop
application.MainLoop()
I confess to have some lost track, I have to inherit from BitmapButton class, right, What is wrong?
joh
I have wxPython 2.8. There is no GenBitmapToggleButton
or miss I something?
You don't. Just use GenBitmapToggleButton, see wxPython demo for an example (in Custom Controls/GenericButtons).
EDIT: Present since at least 2.8.9.1, see Link. Have a closer look at the wxPython demo if unsure how to use.
Related
I subclassed a QStyledItemDelegate to change the highlight color of the selections, and if there is a QColor in ForegroundRole and BackgroundRole I will try to blend two colors.
However, when I'm trying to select a row in the tableview, following error message shows up:
TypeError: arguments did not match any overloaded call:
setColor(self, QPalette.ColorGroup, QPalette.ColorRole, Union[QColor, Qt.GlobalColor, QGradient]): argument 1 has unexpected type 'ColorRole'
setColor(self, QPalette.ColorRole, Union[QColor, Qt.GlobalColor, QGradient]): argument 2 has unexpected type 'NoneType'
and terminal shows:
QPainter::begin: A paint device can only be painted by one painter at a time.
QPainter::setCompositionMode: Painter not active
QBackingStore::endPaint() called with active painter; did you forget to destroy it or call QPainter::end() on it?
Here is my implementation:
class StyleDelegateForTable_List(QStyledItemDelegate):
"""
Customize highlight style for ListView & TableView
"""
def __init__(self, parent):
super().__init__(parent)
self.hightColor = QtGui.QColor("#0096ff")
def paint(self, painter, option: QtWidgets.QStyleOptionViewItem, index):
self.initStyleOption(option, index)
if (option.state & QtWidgets.QStyle.StateFlag.State_Selected and
option.state & QtWidgets.QStyle.StateFlag.State_Active):
# get foreground color,
# don't know where to set foreground color though...
fg = self.getColor(index, isBG = False)
# get background color
bg = self.getColor(index, isBG = True)
# set highlight color
option.palette.setColor(QtGui.QPalette.ColorRole.Highlight,
self.mixColors(bg))
QStyledItemDelegate.paint(self, painter, option, index)
def getColor(self, index: QModelIndex, isBG = True) -> QtGui.QColor:
parentWidget = self.parent()
model = parentWidget.model()
dataRole = Qt.ItemDataRole.BackgroundRole if isBG else Qt.ItemDataRole.ForegroundRole
# TableView
if isinstance(parentWidget, QtWidgets.QTableView):
if isinstance(model, QSortFilterProxyModel):
# proxy model
sourceIndex = model.mapToSource(index)
return model.sourceModel().data(sourceIndex, dataRole)
elif isinstance(model, TestDataTableModel):
# abstract table model
return model.data(index, dataRole)
# ListView
if isinstance(parentWidget, QtWidgets.QListView):
if isinstance(model, QSortFilterProxyModel):
# all of listView uses proxyModel
sourceIndex = model.mapToSource(index)
return model.sourceModel().data(sourceIndex, dataRole)
return QtGui.QColor("#000000")
def mixColors(self, src) -> QtGui.QColor:
if isinstance(src, QtGui.QColor):
r = int(src.red()*0.7 + self.hightColor.red()*0.3)
g = int(src.green()*0.7 + self.hightColor.green()*0.3)
b = int(src.blue()*0.7 + self.hightColor.blue()*0.3)
return QtGui.QColor(r, g, b)
else:
# I intended to mix default bg or fg color
# with highlight color but default is None.
# return hightlight color for now
self.hightColor
I tried to set a breakpoint in getColor, but my app just crashed, what did I do wrong here?
Edit:
This question is invalid, as # musicamante points out, mixColors returns None instead of hightlight color if the original Background/Foreground is None.
I will keep this question in case someone needs the same functionality.
Basically, this is an interactive heatmap but the twist is that the source is updated by reading values from a file that gets updated regularly.
dont bother about the class "generator", it is just for keeping data and it runs regularly threaded
make sure a file named "Server_dump.txt" exists in the same directory of the script with a single number greater than 0 inside before u execute the bokeh script.
what basically happens is i change a number inside the file named "Server_dump.txt" by using echo 4 > Server_dump.txt on bash,
u can put any number other than 4 and the script automatically checks the file and plots the new point.
if u don't use bash, u could use a text editor , replace the number and save, and all will be the same.
the run function inside the generator class is the one which checks if this file was modified , reads the number, transforms it into x& y coords and increments the number of taps associated with these coords and gives the source x,y,taps values based on that number.
well that function works fine and each time i echo a number , the correct rectangle is plotted but,
now I want to add the functionality of that clicking on a certain rectangle triggers a callback to plot a second graph based on the coords of the clicked rectangle but i can't even get it to trigger even though i have tried other examples with selected.on_change in them and they worked fine.
*if i increase self.taps for a certain rect by writing the number to the file multiple times, color gets updated but if i hover over the rect it shows me the past values and not the latest value only .
my bokeh version is 1.0.4
from functools import partial
from random import random,randint
import threading
import time
from tornado import gen
from os.path import getmtime
from math import pi
import pandas as pd
from random import randint, random
from bokeh.io import show
from bokeh.models import LinearColorMapper, BasicTicker, widgets, PrintfTickFormatter, ColorBar, ColumnDataSource, FactorRange
from bokeh.plotting import figure, curdoc
from bokeh.layouts import row, column, gridplot
source = ColumnDataSource(data=dict(x=[], y=[], taps=[]))
doc = curdoc()
#sloppy data receiving function to change data to a plottable shape
class generator(threading.Thread):
def __init__(self):
super(generator, self).__init__()
self.chart_coords = {'x':[],'y':[],'taps':[]}
self.Pi_coords = {}
self.coord = 0
self.pos = 0
self.col = 0
self.row = 0
self.s = 0
self.t = 0
def chart_dict_gen(self,row, col):
self.col = col
self.row = row+1
self.chart_coords['x'] = [i for i in range(1,cla.row)]
self.chart_coords['y'] = [i for i in range(cla.col, 0, -1)] #reversed list because chart requires that
self.chart_coords['taps']= [0]*(row * col)
self.taps = [[0 for y in range(col)] for x in range(row)]
def Pi_dict_gen(self,row,col):
key = 1
for x in range(1,row):
for y in range(1,col):
self.Pi_coords[key] = (x,y)
key = key + 1
def Pi_to_chart(self,N):
x,y = self.Pi_coords[N][0], self.Pi_coords[N][1]
return x,y
def run(self):
while True:
if(self.t == 0):
self.t=1
continue
time.sleep(0.1)
h = getmtime("Server_dump.txt")
if self.s != h:
self.s = h
with open('Server_dump.txt') as f:
m = next(f)
y,x = self.Pi_to_chart(int(m))
self.taps[x][y] += 1
# but update the document from callback
doc.add_next_tick_callback(partial(update, x=x, y=y, taps=self.taps[x][y]))
cla = generator()
cla.chart_dict_gen(15,15)
cla.Pi_dict_gen(15, 15)
x = cla.chart_coords['x']
y = cla.chart_coords['y']
taps = cla.chart_coords['taps']
#gen.coroutine
def update(x, y, taps):
taps += taps
print(x,y,taps)
source.stream(dict(x=[x], y=[y], taps=[taps]))
colors = ["#CCEBFF","#B2E0FF","#99D6FF","#80CCFF","#66c2FF","#4DB8FF","#33ADFF","#19A3FF", "#0099FF", "#008AE6", "#007ACC","#006BB2", "#005C99", "#004C80", "#003D66", "#002E4C", "#001F33", "#000F1A", "#000000"]
mapper = LinearColorMapper(palette=colors, low= 0, high= 15) #low = min(cla.chart_coords['taps']) high = max(cla.chart_coords['taps'])
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
p = figure(title="Tou",
x_range=list(map(str,x)),
y_range=list(map(str,reversed(y))),
x_axis_location="above",
plot_width=900, plot_height=400,
tools=TOOLS, toolbar_location='below',
tooltips=[('coords', '#y #x'), ('taps', '#taps%')])
p.grid.grid_line_color = "#ffffff"
p.axis.axis_line_color = "#ef4723"
p.axis.major_tick_line_color = "#af0a36"
p.axis.major_label_text_font_size = "7pt"
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
p.rect(x="x", y="y",
width=0.9, height=0.9,
source=source,
fill_color={'field': 'taps', 'transform': mapper},
line_color = "#ffffff",
)
color_bar = ColorBar(color_mapper=mapper,
major_label_text_font_size="7pt",
ticker=BasicTicker(desired_num_ticks=len(colors)),
formatter=PrintfTickFormatter(format="%d%%"),
label_standoff=6, border_line_color=None, location=(0, 0))
curdoc().theme = 'dark_minimal'
def ck(attr, old, new):
print('here') #doesn't even print hi in the terminal if i click anywhere
source.selected.on_change('indices', ck)
p.add_layout(color_bar, 'right')
doc.add_root(p)
thread = cla
thread.start()
i wanted even to get a printed hi in the terminal but nothing
You have not actually added any selection tool at all to your plot, so no selection is ever made. You have specified:
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
Those are the only tools that will be added, and none of them make selections, there for nothing will cause source.selection.indices to ever be updated. If you are looking for selections based on tap, you must add a TapTool, e.g. with
TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom,tap"
Note that there will not be repeated callbacks if you tap the same rect multiple times. The callback only fires when the selection changes and clicking the same glyph twice in a row results in an identical selection.
I have been trying to automatically update a variable and run a code snippet after altering a ipywidget.
So far the only partial solution I have found is to declare a global variable kind of following the example on the github (here):
import ipywidgets as widgets
from IPython.display import display
x = 5
slider = widgets.IntSlider()
slider.value = x
def on_change(v):
global x
x = v['new']
slider.observe(on_change, names='value')
display(slider)
Ideally what I am trying to achieve is to automatically change the x value after altering the widget without the use of global variables and also change some previously defined variables. It would be something like this:
x = 5
y = []
slider = widgets.IntSlider()
slider.value = x
def on_change(v):
x = v['new']
y.append(x)
slider.observe(on_change, names='value')
display(slider)
One way to achieve this would to be to make a class that accepts the new widget value, and does the derived calculation as well. I've done a simple addition but you could append to a list.
import ipywidgets as widgets
class Updated:
def __init__(self):
self.widget_value = None
self.derived_value = None
def update(self, val_dict) -> None:
self.widget_value = val_dict['new']
self.derived_value = self.widget_value + 10
update_class = Updated()
x = 5
y = []
slider = widgets.IntSlider()
slider.value = x
def on_change(v):
update_class.update(v)
slider.observe(on_change, names='value')
display(slider)
You can then see how update_class.widget_value and update_class.derived_value change as you move the slider.
I've been searching all morning for what should be the simplest thing ever but I'm either not looking in the right places or not understanding the answers I'm reading as everything I've tried has failed so far.
I'm creating a dialog with a label, a text input box and a Save button. The user enters text in the box, clicks on save and the text is saved to a variable and the dialogue should close. I can't get the last bit to happen. The closest I've come is destroying the button and the grey background but leaving the panel itself intact. I'm looking for some sort of KillParent solution... My main issue is that when I click the button I need two separate things to happen (save and exit) so when I call the on click function once I have done the variable saving I no longer have control to kill the main window. I know it's really elementary stuff but I just can't figure it out.
class ShowConnString(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.saveButton =wx.Button(self, label="Save", pos=(360, 50))
self.lblname = wx.StaticText(self, label="ConnectionString:", pos=(20,20))
self.editname = wx.TextCtrl(self, value="server='(local)', database='Audit', uid='sa', pwd='_PWD4sa_'", pos=(125, 18), size=(600,-1))
self.saveButton.Bind(wx.EVT_BUTTON, self.SaveConnString)
def SaveConnString(self, event):
self.editname.SelectAll()
self.connstringtext = self.editname.GetStringSelection()
print (self.connstringtext)
Its a bit messy but I don't have time to clean it up at the moment, it should use a sizer in the showconnstring class.
import wx
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "Dialog Test",size=(500,400))
self.panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
self.log = wx.TextCtrl(self.panel, wx.ID_ANY, size=(400,300),style = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL)
self.button = wx.Button(self.panel, label="Click me")
sizer.Add(self.log, 0, wx.EXPAND | wx.ALL, 10)
sizer.Add(self.button, 0, wx.EXPAND | wx.ALL, 10)
self.panel.SetSizer(sizer)
self.Bind(wx.EVT_BUTTON, self.OnButton)
def OnButton(self,event):
dlg = ShowConnString(parent = self.panel)
dlg.ShowModal()
if dlg.result_name:
self.log.AppendText("Name: "+dlg.result_name+"\n")
self.log.AppendText("Spin: "+str(dlg.result_spin)+"\n")
self.log.AppendText("Choice: "+str(dlg.result_choice)+"\n")
else:
self.log.AppendText("No selection made\n")
dlg.Destroy()
class ShowConnString(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent, wx.ID_ANY, "Save", size= (650,220))
self.panel = wx.Panel(self,wx.ID_ANY)
self.lblname = wx.StaticText(self.panel, label="Connection", pos=(20,20))
self.editname = wx.TextCtrl(self.panel, value="server=127.0.0.1", pos=(110,20), size=(500,-1))
self.lbl_1 = wx.StaticText(self.panel, wx.ID_ANY, ("Spin Control"), pos=(20,60))
self.spin = wx.SpinCtrl(self.panel, wx.ID_ANY, "", min=1, max=10, pos=(110,60))
self.lbl_2 = wx.StaticText(self.panel, wx.ID_ANY, ("Choice"),pos=(20,100))
self.choice = wx.Choice(self.panel, wx.ID_ANY, choices=[("Choice 1"), ("Choice 2"), ("Choice 3")], pos=(110,100))
self.saveButton =wx.Button(self.panel, label="Save", pos=(110,160))
self.closeButton =wx.Button(self.panel, label="Cancel", pos=(210,160))
self.saveButton.Bind(wx.EVT_BUTTON, self.SaveConnString)
self.closeButton.Bind(wx.EVT_BUTTON, self.OnQuit)
self.Bind(wx.EVT_CLOSE, self.OnQuit)
self.spin.SetValue(0)
self.choice.SetSelection(0)
self.Show()
def OnQuit(self, event):
self.result_name = None
self.Destroy()
def SaveConnString(self, event):
self.result_name = self.editname.GetValue()
self.result_spin = self.spin.GetValue()
self.result_choice = self.choice.GetSelection()
self.Destroy()
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()
It appears that you are attempting to sub-class a wx.TextEntryDialog but using wx.Panel.
You will probably find much simpler to just use a wx.TextEntryDialog directly i.e.
import wx
app = wx.App()
dlg = wx.TextEntryDialog(None,"Connection String:","Save","server='(local)', database='Audit', uid='sa', pwd='_PWD4sa_'")
dlg.SetSize((600,180))
if dlg.ShowModal() == wx.ID_OK:
text = dlg.GetValue()
print text
dlg.Destroy()
I am wondering how to best truncate text in a QLabel based on it's maximum width/height.
The incoming text could be any length, but in order to keep a tidy layout I'd like to truncate long strings to fill a maximum amount of space (widget's maximum width/height).
E.g.:
'A very long string where there should only be a short one, but I can't control input to the widget as it's a user given value'
would become:
'A very long string where there should only be a short one, but ...'
based on the required space the current font needs.
How can I achieve this best?
Here is a simple example of what I'm after, though this is based on word count, not available space:
import sys
from PySide.QtGui import *
from PySide.QtCore import *
def truncateText(text):
maxWords = 10
words = text.split(' ')
return ' '.join(words[:maxWords]) + ' ...'
app = QApplication(sys.argv)
mainWindow = QWidget()
layout = QHBoxLayout()
mainWindow.setLayout(layout)
text = 'this is a very long string, '*10
label = QLabel(truncateText(text))
label.setWordWrap(True)
label.setFixedWidth(200)
layout.addWidget(label)
mainWindow.show()
sys.exit(app.exec_())
Even easier - use the QFontMetrics.elidedText method and overload the paintEvent, here's an example:
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QApplication,\
QLabel,\
QFontMetrics,\
QPainter
class MyLabel(QLabel):
def paintEvent( self, event ):
painter = QPainter(self)
metrics = QFontMetrics(self.font())
elided = metrics.elidedText(self.text(), Qt.ElideRight, self.width())
painter.drawText(self.rect(), self.alignment(), elided)
if ( __name__ == '__main__' ):
app = None
if ( not QApplication.instance() ):
app = QApplication([])
label = MyLabel()
label.setText('This is a really, long and poorly formatted runon sentence used to illustrate a point')
label.setWindowFlags(Qt.Dialog)
label.show()
if ( app ):
app.exec_()
I found that #Eric Hulser's answer, while great, didn't work when the label was put into another widget.
I came up with this by hacking together Eric's response with the Qt Elided Label Example. It should behave just like a regular label, yet elide horizontally when the text width exceeds the widget width. It has an extra argument for different elide modes. I also wrote some tests for fun :)
If you want to use PyQt5...
Change "PySide2" to "PyQt5"
Change "Signal" to "pyqtSignal"
Enjoy!
Eliding Label
# eliding_label.py
from PySide2 import QtCore, QtWidgets, QtGui
class ElidingLabel(QtWidgets.QLabel):
"""Label with text elision.
QLabel which will elide text too long to fit the widget. Based on:
https://doc-snapshots.qt.io/qtforpython-5.15/overviews/qtwidgets-widgets-elidedlabel-example.html
Parameters
----------
text : str
Label text.
mode : QtCore.Qt.TextElideMode
Specify where ellipsis should appear when displaying texts that
don’t fit.
Default is QtCore.Qt.ElideMiddle.
Possible modes:
QtCore.Qt.ElideLeft
QtCore.Qt.ElideMiddle
QtCore.Qt.ElideRight
parent : QWidget
Parent widget. Default is None.
f : Qt.WindowFlags()
https://doc-snapshots.qt.io/qtforpython-5.15/PySide2/QtCore/Qt.html#PySide2.QtCore.PySide2.QtCore.Qt.WindowType
"""
elision_changed = QtCore.Signal(bool)
def __init__(self, text='', mode=QtCore.Qt.ElideMiddle, **kwargs):
super().__init__(**kwargs)
self._mode = mode
self.is_elided = False
self.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
self.setText(text)
def setText(self, text):
self._contents = text
# This line set for testing. Its value is the return value of
# QFontMetrics.elidedText, set in paintEvent. The variable
# must be initialized for testing. The value should always be
# the same as contents when not elided.
self._elided_line = text
self.update()
def text(self):
return self._contents
def paintEvent(self, event):
super().paintEvent(event)
did_elide = False
painter = QtGui.QPainter(self)
font_metrics = painter.fontMetrics()
text_width = font_metrics.horizontalAdvance(self.text())
# layout phase
text_layout = QtGui.QTextLayout(self._contents, painter.font())
text_layout.beginLayout()
while True:
line = text_layout.createLine()
if not line.isValid():
break
line.setLineWidth(self.width())
if text_width >= self.width():
self._elided_line = font_metrics.elidedText(self._contents, self._mode, self.width())
painter.drawText(QtCore.QPoint(0, font_metrics.ascent()), self._elided_line)
did_elide = line.isValid()
break
else:
line.draw(painter, QtCore.QPoint(0, 0))
text_layout.endLayout()
if did_elide != self.is_elided:
self.is_elided = did_elide
self.elision_changed.emit(did_elide)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
long_text = "this is some long text, wouldn't you say?"
elabel = ElidingLabel(long_text)
elabel.show()
app.exec_()
Test Eliding Label
# test_eliding_label.py.py
#
# Run tests with
#
# python3 -m unittest test_eliding_label.py --failfast --quiet
import unittest
import unittest.mock
from PySide2 import QtCore, QtWidgets, QtGui, QtTest
import eliding_label
if not QtWidgets.QApplication.instance():
APP = QtWidgets.QApplication([]) # pragma: no cover
class TestElidingLabelArguments(unittest.TestCase):
def test_optional_text_argument(self):
elabel = eliding_label.ElidingLabel()
self.assertEqual(elabel.text(), "")
def test_text_argument_sets_label_text(self):
elabel = eliding_label.ElidingLabel(text="Test text")
self.assertEqual(elabel.text(), "Test text")
def test_optional_elision_mode_argument(self):
elabel = eliding_label.ElidingLabel()
self.assertEqual(elabel._mode, QtCore.Qt.ElideMiddle)
class TestElidingLabel(unittest.TestCase):
def setUp(self):
self.elabel = eliding_label.ElidingLabel()
def test_elabel_is_a_label(self):
self.assertIsInstance(self.elabel, QtWidgets.QLabel)
def test_has_elision_predicate(self):
self.assertEqual(self.elabel.is_elided, False)
def test_elision_predicate_changes_when_text_width_exceeds_widget_width(self):
# NOTE: This is a bit of a stretch, inducing a paint event
# when the event loop isn't running. Throws a bunch of C++
# sourced text which can't be (easily) caught.
self.elabel.setFixedWidth(25)
self.assertEqual(self.elabel.width(), 25)
long_text = "This is line is definely longer than 25 pixels."
painter = QtGui.QPainter()
font_metrics = painter.fontMetrics()
long_text_width = font_metrics.horizontalAdvance(long_text)
self.assertGreater(long_text_width, 25)
self.elabel.setText(long_text)
x = self.elabel.x()
y = self.elabel.y()
w = self.elabel.width()
h = self.elabel.height()
paint_event = QtGui.QPaintEvent(QtGui.QRegion(x, y, w, h))
self.elabel.paintEvent(paint_event)
self.assertEqual(self.elabel.is_elided, True)
def test_text_is_elided_when_text_width_exceeds_widget_width(self):
# NOTE: This is a bit of a stretch, inducing a paint event
# when the event loop isn't running. Throws a bunch of C++
# sourced text which can't be (easily) caught.
self.elabel.setFixedWidth(25)
self.assertEqual(self.elabel.width(), 25)
long_text = "This is line is definely longer than 25 pixels."
painter = QtGui.QPainter()
font_metrics = painter.fontMetrics()
long_text_width = font_metrics.horizontalAdvance(long_text)
self.assertGreater(long_text_width, 25)
self.elabel.setText(long_text)
x = self.elabel.x()
y = self.elabel.y()
w = self.elabel.width()
h = self.elabel.height()
paint_event = QtGui.QPaintEvent(QtGui.QRegion(x, y, w, h))
self.elabel.paintEvent(paint_event)
# PySide2.QtGui.QFontMetrics.elidedText states, "If the string
# text is wider than width , returns an elided version of the
# string (i.e., a string with '…' in it). Otherwise, returns
# the original string."
self.assertEqual(self.elabel._elided_line, '…')
def test_text_is_not_elided_when_text_width_is_less_than_widget_width(self):
# NOTE: This is a bit of a stretch, inducing a paint event
# when the event loop isn't running. Throws a bunch of C++
# sourced text which can't be (easily) caught.
self.elabel.setFixedWidth(500)
self.assertEqual(self.elabel.width(), 500)
short_text = "Less than 500"
painter = QtGui.QPainter()
font_metrics = painter.fontMetrics()
short_text_width = font_metrics.horizontalAdvance(short_text)
self.assertLess(short_text_width, 500)
self.elabel.setText(short_text)
x = self.elabel.x()
y = self.elabel.y()
w = self.elabel.width()
h = self.elabel.height()
paint_event = QtGui.QPaintEvent(QtGui.QRegion(x, y, w, h))
self.elabel.paintEvent(paint_event)
# PySide2.QtGui.QFontMetrics.elidedText states, "If the string
# text is wider than width , returns an elided version of the
# string (i.e., a string with '…' in it). Otherwise, returns
# the original string."
self.assertEqual(self.elabel._elided_line, short_text)
def test_stores_full_text_even_when_elided(self):
# NOTE: This is a bit of a stretch, inducing a paint event
# when the event loop isn't running. Throws a bunch of C++
# sourced text which can't be (easily) caught.
self.elabel.setFixedWidth(25)
self.assertEqual(self.elabel.width(), 25)
long_text = "This is line is definely longer than 25 pixels."
painter = QtGui.QPainter()
font_metrics = painter.fontMetrics()
long_text_width = font_metrics.horizontalAdvance(long_text)
self.assertGreater(long_text_width, 25)
self.elabel.setText(long_text)
x = self.elabel.x()
y = self.elabel.y()
w = self.elabel.width()
h = self.elabel.height()
paint_event = QtGui.QPaintEvent(QtGui.QRegion(x, y, w, h))
self.elabel.paintEvent(paint_event)
# PySide2.QtGui.QFontMetrics.elidedText states, "If the string
# text is wider than width , returns an elided version of the
# string (i.e., a string with '…' in it). Otherwise, returns
# the original string."
self.assertEqual(self.elabel._elided_line, '…')
self.assertEqual(self.elabel.text(), long_text)
def test_has_elision_changed_signal(self):
self.assertIsInstance(self.elabel.elision_changed, QtCore.Signal)
def test_elision_changed_signal_emits_on_change_to_is_elided_predicate(self):
mock = unittest.mock.Mock()
self.elabel.elision_changed.connect(mock.method)
# NOTE: This is a bit of a stretch, inducing a paint event
# when the event loop isn't running. Throws a bunch of C++
# sourced text which can't be (easily) caught.
# Induce elision
self.elabel.setFixedWidth(150)
self.assertEqual(self.elabel.width(), 150)
long_text = "This line is definitely going to be more than 150 pixels"
painter = QtGui.QPainter()
font_metrics = painter.fontMetrics()
long_text_width = font_metrics.horizontalAdvance(long_text)
self.assertGreater(long_text_width, 150)
self.elabel.setText(long_text)
self.assertEqual(self.elabel.is_elided, False) # no elide until painting
x = self.elabel.x()
y = self.elabel.y()
w = self.elabel.width()
h = self.elabel.height()
paint_event = QtGui.QPaintEvent(QtGui.QRegion(x, y, w, h))
self.elabel.paintEvent(paint_event)
self.assertEqual(self.elabel.is_elided, True)
mock.method.assert_called_once()
# Remove elision
short_text = "Less than 150"
painter = QtGui.QPainter()
font_metrics = painter.fontMetrics()
short_text_width = font_metrics.horizontalAdvance(short_text)
self.assertLess(short_text_width, 150)
self.elabel.setText(short_text)
self.assertEqual(self.elabel.is_elided, True) # still elided until painting
x = self.elabel.x()
y = self.elabel.y()
w = self.elabel.width()
h = self.elabel.height()
paint_event = QtGui.QPaintEvent(QtGui.QRegion(x, y, w, h))
self.elabel.paintEvent(paint_event)
self.assertEqual(self.elabel.is_elided, False)
self.assertEqual(mock.method.call_count, 2)
You can achieves this through determining the width with QFontMetrics, see this answer.
You would probably want to use or create some algorithm which finds the place to cut quickly, unless doing it in a simple for loop would be sufficient.
simpler solution if you want show QLabel in center in provided area
label.setAlignment(Qt.AlignmentFlag.AlignCenter)
label.minimumSizeHint = lambda self=label: QSize(0, QLabel.minimumSizeHint(self).height() )