How can I limit the width of a QLineEdit [duplicate] - python

I am working with a GUI based on PySide. I made a (one line) text box with QLineEdit and the input is just four characters long, a restriction I already successfully applied.
The problem is I have a wider than needed text box (i.e. there is a lot of unused space after the text). How can I shorten the length of the text box?
I know this is something that is easily fixed by designing the text box with Designer; however, this particular text box is not created in Designer.

If what you want is modify your QLineEdit width and fix it, use:
#setFixedWidth(int w)
MyLineEdit.setFixedWidth(120)

Looking at the source of QLineEdit.sizeHint() one sees that a line edit is typically wide enough to display 17 latin "x" characters. I tried to replicate this in Python and change it to display 4 characters but I failed in getting the style dependent margins of the line edit correctly due to limitations of the Python binding of Qt.
A simple:
e = QtGui.QLineEdit()
fm = e.fontMetrics()
m = e.textMargins()
c = e.contentsMargins()
w = 4*fm.width('x')+m.left()+m.right()+c.left()+c.right()
is returning 24 in my case which however is not enough to display four characters like "abcd" in a QLineEdit. A better value would be about 32 which you can set for example like.
e.setMaximumWidth(w+8) # mysterious additional factor required
which might still be okay even if the font is changed on many systems.

Related

How to find the real first line of a shape in pptx presentation

At the moment I need to get the actual width of the text, the best solution that I tried was to find the first line of text, and get its width.
The presentations given to me were made by different people, and I cannot directly influence them. And it turns out that the shape frame itself in the presentation is often much wider than the text, which is a problem, because I need to get visible text shape collisions, which possible only when i have real frame of text(tried to show it in the screenshot1, screenshot2)
My best try to get real first line is:
# i have already compiled PIL font, with font typeface and size named "font"
# width of shape and text of shape
# and also class TextWrapper that wraps given string with given width and outputs a list
# get first line from wrapped text, and from tuple select width
width_first = font.getsize(TextWrapper(shape.text, font, width).text_lines[0])[0]
# get lines that wrapped at width first string
wrapped_lines = TextWrapper(shape.text, font, width_first).text_lines
# ... some calculations here
Problem that a wrap symbol like '\n' not always be in text, but in presentation i see that wrap.
I tried to explain how I could, did anyone come across this at all?
EDIT:
I found some way how to do thig that i need.
If u need something equals, see code.
import win32com.client
Application = win32com.client.Dispatch("PowerPoint.Application")
# WithWindow=False forces PowerPoint to do not open the PowerPoint Window
Presentation = Application.Presentations.Open("ABCPATH/to/presentation.pptx",
WithWindow=False)
for Slide in Presentation.Slides:
for Shape in Slide.Shapes:
if Shape.HasTextFrame: # checks if shape has text, becouse we avoiding imgs
# that what we need
first_line = Shape.TextFrame.TextRange.Lines(1, 1)
You must have installed PowerPoint Application to do that, and installed pywin32
And it works only on Windows, so thats not so good choose, but for me it works perfect
Maybe someone found this usable

Python - print image bit data to a ESC/POS printer with python

I've been looking for an example of how to format and print bmp's to my receipt printer (so I can add logos) for a long time, so I doubt this is a duplicate post considering others were for java or other script languages. Usually I'm pretty good at understanding instructions, but all I seem to find is the same old instructions I can never fully understand.
I am using python 2.7 and I have a function pI(x) which uses win32print to send data to the printer, where x is the data in string format using "\x??" for hex data like formatting text. It seems to work well.
The programmer manual that came with my printer says (for downloading bit image, GS *) for syntax:
Hex 1D 2A x y d1...dk
and:
d=1 for printing the corresponding dot and d=0 for not printing the corresponding dot.
Here is my questions about these instructions:
Does this mean that all x, y, d1...dk is in hex (or "\x??")? I think so.
What is x and y representing? I read a while ago on a site (maybe this one) that x+y*255 = image width, and I assume that is using the order of operations. Is this correct?
The instructions on my generic printer also state that x and y are both supposed to be between 1 and 48, totaling no more than 1500, unlike some manuals which say x is supposed to be between 0 and 3 and y being between 1 and 128 (I think) which said x+y*255=width, totaling about 2000. it also says k=x*y*8 which I think that the example would be 8*8*8=512*"\x01", so where does the third 8 come from and how do I code that in the string??? Does x=width and y=height?... then how do I get an image width of the maximum 384 dots?
Does this mean that I have to enter "\x00" or "\x01" for each dot, so one instance (a small black block of 8x8) of GS* would be 64*"\x01"?
Do I have to GS * each group of 8 dots tall or line of 8 dots, or will that overwrite the previously programmed data?
I'd like to later include in my program a means of easily creating logos using a tkinter canvas widget and saving it to a text file for future printing using pI(), so I really need to know how to directly 'download' image data to the printer and using a third party module probably won't work since I want to continue using my pI() function. Yes, it's ambitious and I'm probably doing it the hard way. But I'm afraid if I start incorporating too much new stuff I'm not familiar with, I'll get too confused.
Basically, what string should I send to pI() to download an image of a solid 8x8-dot black box with a 2x wide white line down the center on the printer?
Here's an example of what I would like the printer to print so I can see a working code string

In Python Tkinter, how do I set an OptionMenu to a fixed size shorter than the longest item?

As simple as it sounds, I have an Option menu with some 5 character terms and one very long one. When the long option is selected, the window stretches out and it looks atrocious. Setting the width or sticky=EW only works if that width is greater than the length of the longest term.
Ideally, I'd like to show 15 characters max followed by a "..." if it is longer.
Any ideas? Thanks.
I think you're looking for the more powerful "Combobox" in ttk (also in standard python as simple extensions to Tk).
As effbot puts it
The option menu is similar to the combobox
For example:
from tkinter.ttk import Combobox # python 3 notation
combo = Combobox(root,values=['a','aa','aaaaa','aaaaaaa'],width=3)
It will simply cut off the elements that are too long
(If you're in python 2 it's slightly different to import ttk)
If you want a nice "..." to appear when cutting off entries, I think you're best bet is to
elements = ['a','aa','aaaaaaaa']
simple_values = [ e[:3] + ('...' if len(e) > 3 else '') for e in elements]
combo = Combobox(root,values=simple_values )
If you need to be able to map between them, use your favorite data structure or reference by index not value in the combobox

Output colored, right aligned text into the terminal

I want to output colored text completly aligned to the right in the terminal (like in this screenshot of pacman (packet manager of the arch linux distribution)(not colored))
Currently I'm using format:
import shutil
left = "foo"
right = "bar"
width = shutil.get_terminal_size().columns
template = "{left:30}{right:{width}}".format(left=left, right=right, width=width-30)
click.echo(template)
# click.echo works just like print with some additional features
This works great until I add colors via ANSI escape codes:
left = click.style("foo", fg="red")
right = click.style("bar", fg="green")
# click.style just adds ANSI codes for colors and bold etc.
Which looks like this:
I.e. the right side is not completly right aligned. Which is "right", because right is actually \\x1b[32mbar\\x1b[0m which of course has a higher lenght than bar and thus needs less spaces to be right aligned. Until the terminal gets the text and only displays bar (with color).
Am I missing anything in the python std lib or click? Or is there a simple library that deals with terminal colors and alignment that could help me? Or is there a simple solution to this problem?
click's documentation does not mention alignment (which is why you are using python's built-in string class). You could stay within the current set of interfaces by telling your script to remember the lengths of the strings before calling click.style, and adding the difference to the width used for the format call. (This would not work as well if you were centering text).
There are perhaps other libraries, but you could use the curses interface with the filter function to draw single-line displays.
First, I want to figure out that click provide a way to get the terminal size: click.get_terminal_size, the documentation is here
>>> import click
>>> click.get_terminal_size()
>>> (66, 24) # (width, height)
And my solution, should works even you resize the terminal:
width = click.get_terminal_size()[0]
left = click.style("foo", fg="red")
right = click.style("bar", fg="green")
print "{0:}{1:>{2}}".format(left, right, width+6)
Since right actually is \x1b[32mbar\x1b[0m, we increase the width by hand to avoid the problem.
Edit: PyFormat is useful for me when do string format in python. It helps me understand string formating. Hope it will help you.

Qt custom form for QGIS and autocompleted data entering with “validation check”

In Qt-Designer I created a custom form where I can choose several species names in two different combo boxes. This combo boxes are linked via "value relation" to the accordant non spatial tables (gattung and art) in my SpatiaLite database. The whole species name consists of "gattung" and "art". Because these tables ("gattung" and "art") contain a lot of species names (latin) which one can choose I think about an eased editing feature.
1) I'd like to generate an autocompletion when entering the first 3 characters into a line edit (instead of a combo box) for "gattung" so that on can choose only these species that beginns for example with "Que" from a list.
2) When I've chosen a name that matches with these 3 characters for example "Que" for "Quercus", in the second field "art" only valid names can be chosen. For example "petrea" ore "robur" as a subset of the whole "art-names" which one can choose. Valid names are "Quercus robur" or "Quercus petrea". This could be done via line edit when entering 4 characters or with a combo box.
I thought about python and a lettercode (Quepetr for Quercus petrea) to solve this but doesn't know where to start and also where to enter the code for the widgets in QT-Designer.
I'm grateful for any help or idea!
I had a similar problem. I ended up using QLineEdit in combination with QCompleter as explained here. QCompleter is quite powerful for (very) long lists (like species names).
For validation you could use Nathan Woodrows solution here.

Categories