Integrating a LCD Display in a widget that shows the line number of the textEdit - pyqt4

I want to get the line number of a textEdit in pyqt when the mouse cursor is clicked on a particular line . Kindly suggest

Maybe this gives you an idea:
self.connect(self.ui.textEdit, QtCore.SIGNAL("cursorPositionChanged()"),self.lineNumber)
and later:
def lineNumber(self):
# we try to get the lineNumber on change of cursor position
theLineNumber = self.ui.textEdit.textCursor().blockNumber()
self.ui.label.setText('Ln : '+str(theLineNumber))

Related

Sublime Text 3 auto-Indentation with comments

I have been using ST3 to write python for a while, but the auto-indentation has confused me sometimes.
I am currently writing a python function and I noticed that if there is a comment at the end of a line of code, the auto-indentation by pressing enter at middle of the line won't work. For example, I have the following lines:
fig = plt.figure(num = 0, figsize = (10,10))
fig = plt.figure(num = 0, figsize = (10,10)) #This creates a figure
By pressing 'Enter' before 'figsize' in both lines, the first line would auto-indent while the second wouldn't:
Indent Issue Image
Does anyone has any solution to this? I can add the comment after using the indentation but it is always a fuss to do this.

How to begin highlighting words

I am writing an app of a Python editor of my own. I am using a Text widget and want to highlight words as the words are typed in like Python editor. As soon as the character # is typed in, I want to begin highlighting all characters followed from the character # with color of red.
Below is the partial code for the purpose. When the character # was identified as it was typed in to the Text widget, I added a tag of "CM" from the typed-in-character to the end of the line (I thought this would do the job for me).
import tkinter as tk
def onModification(event=None):
c=event.char
if not c: return
pos=hT0.index(tk.INSERT)
if c=='#':
hT0.tag_add('CM',pos,f'{int(pos.split(".")[0])}.end')
return
hW=tk.Tk()
hT0=tk.Text(hW,wrap='none',font=('Times New Roman'12))
hT0.insert('end','')
hT0.place(x=27, y=0, height=515,width=460)
hT0.bind('<Key>', onModification)
hT0.tag_config('CM', foreground='#DD0000')
But the output highlights only characters already existed even without the just-typed-in-character #.
An idea for the job I want?
Thank you so much in advance.
I obtained an idea from the Get position in tkinter Text widget
def onModification(event=None):
...
pos=hT0.index(tk.INSERT)
lineN, ColN=[int(c) for c in pos.split('.')]
if c=='#':
#hT0.tag_add('CM',pos,f'{int(pos.split(".")[0])}.end')
hT0.tag_add('CM',f'{lineN}.{ColN-1}',f'{lineN}.end')
return
...
#hT0.binds('<key>', onModification) needs to be changed to...
hT0.bindtags(('Text','post-class-bindings','.','all'))
hT0.bind_class('post-class-bindings', '<KeyPress>', onModification)

opening a large text file on Label or Text widget in tkinter

I'm working a tkinter application, i want to display a read only text file, whenever i add the text file using label or text, the file is not oranised at all
I want to open a text file in Label or Text(which ever works), but what I keep getting is a text that skids beyond my window frame. I added the scroll button, it's still doing the same thing. I want to the text to be well ordered in a specified Label/Text widgets(read only). thank you in advance.
from tkinter import *
root = Tk()
text_file = open("C:\\Users\stone's\Desktop\\works.txt")
text1 = text_file.read()
for i in text1:
if len(text1)==50:
## MOVE TO NEXT LINE
Label(root, text="%s" % ('\n'),
font=('Bradley Hand ITC', '25', 'bold'),
bg='#c9e3c1').pack()
else:
## DON'T MOVE OVER TO NEXT LINE
Label(root, text="%s" % (i), font=('Bradley Hand ITC',
'25', 'bold'
),
bg='#c9e3c1').pack(side = LEFT)
## ALL I'M TRYING TO DO IS TO SHOW A TEXT ON A LABEL APPROPRIATELY
## WITHOUT THE TEXTS SKIDDING OUT OF THE WINDOW FRAME
root.mainloop()
As said in the answers in this question the question is for python 2 however the comment I linked shows it for python 3 so you can make the text widget readonly.
As a side note: labels are for displaying small pieces of text as labels for other elements not displaying a whole file read only.

Trying to add text from a random line from a text file

I am making a game in pygame for my sister for practising in pygame and I am trying to add a random line from a text file in to window name:
import pygame,random,sys
RandomMess = open('GameText.txt')
pygame.display.set_caption('Dream Land: {}').
#The .txt file currently has 4 lines but will be a lot more in the future...
#These are the test lines: This is in beta, hi :), hello, hi
I have all the rest of the code in case anyone says that I haven't done any other code to make the window or anything.
I want it to say: Dream Land: {} then in the braces add a random line from the file.
You're looking for readlines() and random.choice().
import random,sys
RandomMess = open('GameText.txt')
# .readlines() will create a list of the lines in the file.
# So in our case ['This is in beta', 'hi :)', 'hello', 'hi']
# random.choice() then picks one item from this list.
line = random.choice(RandomMess.readlines())
# Then use .format() to add the line into the caption
pygame.display.set_caption('Dream Land: {}'.format(line))

How to copy and paste changed some lines in (command mode) in Vim

I has such code:
def foo
puts "foo"
end
and as result I need:
def foo
puts "foo"
end
def bar
puts "bar"
end
I would like to perform this in command mode (could you also refer some help?) but other solutions are also welcome.
To copy / paster use type : (with you cursor on def foo line)
y3yGP
It will copy the 3 lines at the end of the file. use xG where x is a line number to go at line x. (Use set number to see line number)
Then you can change foo in bar with command :
:x,ys/foo/bar/
With x the first line of the block, and y the last one :)
Hoping that helps you :)
Staying in insert mode I do the following (cursor on def foo):
S-vjjjyPgv:s/foo/bar/g
Nice trick for me is using gv to retrive last selection.
One can use the following Ex command (assuming that the cursor is
located on the def line).
:,/^end/t//|'[,']s/\<foo\>/bar/g|'[pu!_
To jump to the pairing end line, one can take advantage of the
% command extended by the matchit plugin.
:exe"norm V%y']o\ep"|'[,']s/\<foo\>/bar/g

Resources