The code posted below creates QTextBrowser window filling it with 100 lines of text: starting from MESSAGE-0000 all the way to MESSAGE-0099
from PyQt4 import QtCore, QtGui
app=QtGui.QApplication([])
textBrowser = QtGui.QTextBrowser()
for i in range(100):
textBrowser.insertPlainText('MESSAGE-%04d'%i + '\n')
textBrowser.show()
app.exec_()
Question: How to find a line number where its text says: MESSAGE-0051, then select or highlight it and then scroll to it so the selected-highlightet line is positioned at the top edge of the QTextBrowser window, so the result would look like this:
How to achieve it?
If you search backwards, it will automatically scroll the selected line to the top of the viewport:
textBrowser.moveCursor(QtGui.QTextCursor.End)
textBrowser.find('MESSAGE-0051', QtGui.QTextDocument.FindBackward)
(Of course, if you search for say, MESSAGE-0095, it won't put the selected line at the top, because the view cannot scroll down that far).
Related
I am creating a code editor as my project, and I think it is nice to have vertical lines under tabs so its easier for the coder. I want to draw a vertical line below tabs (I think the image below will be better to understand what I want, Just a screenshot from vs code):
Here you can see that there is a vertical line under def and I think these are pretty useful for me so that I won't make any indentation errors while I am coding. Now I want exactly a feature like this in my code editor. To simplify things I will upload a sample code below.
from tkinter import *
root = Tk()
txt = Text(root) # What changes do I have to make to this text widget so the lines appear like it does in the image
txt.pack()
root.mainloop()
The text widget doesn't directly support this. You can fake it by using ascii line drawing characters, but it would probably be a lot of work. It should be doable, though.
You can do this if you base your IDE on a canvas rather than a Text widget, but the text-editing ability of the canvas text item would be very cumbersome compared to the text widget.
All of that being said, with a little creativity you can get something close to what you want with tags. The idea is to add a tag to the character that you want to appear as a line. If you set the tag to have a different color than the text, it will look like a vertical line. The problem, however, is that you don't have much control over the width of the line. You might have some luck using the bgstipple option, which allows you to apply a bitmap to the region.
Here's an example to illustrate the idea.
import tkinter as tk
import tempfile
def add_markup(text, start=None, end="end"):
text.mark_set("current", start or "1.0")
text.tag_remove("markup", "current", end)
while text.compare("current", "<", end):
if text.get("current") == " ":
text.tag_add("markup", "current", "current+1c")
text.mark_set("current", "current+4c")
else:
text.mark_set("current", f"current +1l linestart")
root = tk.Tk()
text = tk.Text(root)
text.tag_configure("markup", background='#f0f0f0', bgstipple='#line.xbm', fgstipple='#line.xbm')
text.pack(fill="both", expand=True)
text.bind(
"<Any-KeyRelease>",
lambda event: add_markup(event.widget, "insert linestart", "insert lineend")
)
def load_this_file(text):
with open(__file__, "r") as f:
text.insert("1.0", f.read())
load_this_file(text)
add_markup(text)
root.mainloop()
This is what it looks like on my machine:
I'm trying to show the following code results on webEngineView.setHtml:
from ahp_calculator import ahp_calculator
AC = ahp_calculator()
AC.open_calculator()
So it will be displayed in the right box (instead of folium map) after clicking a button on the left as shown in the figure below:
I found ways to display maps, Tables and Plotes using SetHtml. But how to render and display ahp_calculator?
Just starting to try to use python-pptx, and have fallen at the first. Linux Mint 20.1, python 3.85, LibreOffice 6.4.
This is basically the 'Hello World' from the documentation.
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = width = height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
print('before text ', txBox.left, txBox.top, txBox.width, txBox.height)
tf.text = "This is a long line of text inside a textbox"
print('after text ', txBox.left, txBox.top, txBox.width, txBox.height)
prs.save('test.pptx')
The text is more than a single line for the textbox. Printing out its bounds before and after text insertion shows that as far as python-pptx is concerned, the textbox width hasn't changed.
When the resulting presentation is viewed in LibreOffice, instead of wrapping the text within its boundaries, the textbox has expanded symmetrically around the mid point, pushing the start of the text off the lefthand edge of the page.
I was hoping the LibreOffice was only incompatible with powerpoint for rare edge cases, but text in text boxes is the meat and bread of presentations.
When I upload it to google slides, the text wraps within the left and right text box boundaries, but spills out of the bottom edge. The textbox shows up as 1" x 1" in the right place.
If I use onedrive.live.com, the text is left justified in the box and spills out of the righthand side without wrapping, and the textbox is shown as being 1" x 1" in the right place.
If I use onlinedocumentviewer.com, the display is the same as onedrive, though I can't get to see the text box.
Unfortunately I can't test the behaviour on a native powerpoint installation.
Maybe there's an autosize or fixed flag, which left unset leaves each viewer defaulting it in its own idiosyncratic way? How do we control text boxes / frames when targetting LibreOffice?
I possibly have a workaround to break up my text into single lines and use one per text box, but I'd rather understand the whether it can be done the proper way.
After some floundering around in the docs, I stumbled across the text_frame .word_wrap property. Setting to True gets the text to wrap in LibreOffice. While I was there, setting text_frame.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE reduces font size to get it all to fit in the box.
Is there a list of properties like .word_wrap in one place anywhere?
Is there a way to fractionally increase the spacing of lines in a tkinter text widget?
I'd like to space my text at, say, 1.2 * font size to enable the user to adjust for readability.
The spacing1, spacing2 and spacing3 parameters appear to be platform dependent, not supported on Windows, and whole lines only.
You can configure spacing in pixels as follows:
text.config(spacing1=10) # Spacing above the first line in a block of text
text.config(spacing2=10) # Spacing between the lines in a block of text
text.config(spacing3=10) # Spacing after the last line in a block of text
Works for me on Python 3.6.5 on win10. Or do you have an example of non-working code?
I have created a QMessageBox as follows:
msg_box = QtGui.QMessageBox()
msg_box.setSizeGripEnabled(True)
msg_box.setIcon( QtGui.QMessageBox.Information )
msg_box.setText('The following files are not .jpg ')
msg_box.setInformativeText('No. of Items : {0}'.format(len(contents)))
msg_box.setDetailedText('{0}'.format('\n'.join([str(sel) for sel in img_sels])))
msg_box.setStandardButtons(QtGui.QMessageBox.Cancel)
msg_exec = msg_box.exec_()
User will select a handful of images, and if within the selections, if it consists of items that are not of jpeg/ .jpg format, these items' file path will be collated and be displayed in QMessageBox
One issue that I had is that, I am having difficulties in getting the QMessageBox to be resize or have the UI width conform according the the length or the text etc. As I am unable to do that, the popup ui displays the filepath almost like in a wrapped text format, somewhat unsightly.
Is there any other ways that I can do to improve the code and have it accommodate to the width etc?
If not, is there another QtGui command that I can consider to use it?