Python Forum
[PyQt] Newbe question about Print() to PyQt
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Newbe question about Print() to PyQt
#1
I am looking to porting my python code to PyQt but I don't understand how to "Print()" to a text window. a print fragment of my code is below and it produces this simple user interface screen that I would like to see in a PyQt window instead.

class Start_Up_Display(object):
def __init__(self):
pass

def start(self):
global message
print("\nSWITCH STATUS REPORT")
print("-------------")
num = 0
while num < len(Sw)-1:
screen.show_all_in_line(num)
num = num +1

screen.show_all_in_line(num)


num = 0
if len(message)!= 0:
print("\nMessage: [%s]" % message)
else:
print("\nMessage: ")

message = ''


class Display_Switch(object):
def __init__(self):
pass

def show_all_in_line(self, abb):

print("%s %s %s %s \[email protected] %s" % ( Sw[int(abb)][0].ljust(3),
Sw[int(abb)][3].ljust(3),
chr(int(Sw[int(abb)][4])),
Sw[int(abb)][1].ljust(14),
Sw[int(abb)][2].ljust(41))
)

Result:

[Image: MRSM.jpg]
Reply
#2
Please re-post code between  code tags, see: BBCODE
in addition, when pasting code, use shift-ctrl-v to preserve indentation.
You code doesn't make sense without proper indentation

Also, it's better to use output tags to show results, and not images.
Reply
#3

 
class Start_Up_Display(object):
	def __init__(self):
		pass

	def start(self):
		global message
		print("\nSWITCH STATUS REPORT")
		print("-------------")
		num = 0
		while num < len(Sw)-1:
			screen.show_all_in_line(num)
			num = num +1	

		screen.show_all_in_line(num)			
			

		num = 0			
		if len(message)!= 0:
			print("\nMessage: [%s]" % message)
		else:
			print("\nMessage: ")
		
		message = ''
		

class Display_Switch(object):
	def __init__(self):
		pass
	
	def show_all_in_line(self, abb):
		
		print("%s %s %s  %s \[email protected] %s" % (	Sw[int(abb)][0].ljust(3), 
											Sw[int(abb)][3].ljust(3),
											chr(int(Sw[int(abb)][4])), 
											Sw[int(abb)][1].ljust(14), 
											Sw[int(abb)][2].ljust(41))
											)

"Also, it's better to use output tags"

I don't know what an output tag is.
Reply
#4
If you look at the posting editor, there are 5 icons in a group just after the quote symbol.
They are in order:
  • python code tags
  • error code tags
  • output tags
  • inline code tags (used for short code insert that fits on the current line)
  • Spoiler (hides images, code, etc with a message to 'click here' which enlarges the hidden object)

I'd answer your Qt question, I'm good with wxpython and tkinter, but not a lot of experience with Qt
I know the docs are here: http://doc.qt.io/qt-5/ and I did play with Qt4 from C++, but not python
Someone else will probably pick up from here.
Reply
#5
(Dec-18-2017, 10:32 PM)TimW Wrote: I don't understand how to "Print()" to a text window.

In PYQT there are different widgets each for input, output display, buttons etc.

There is no print() function.{Print just works on Console}
Though widgets can be used to display text on the screen.

eg-
There's a textBrowser widget, who's setText property can be used to display a string.

textbr = QTextBrowser()
textbr.setText("This string will be displayed")
If you're having trouble starting with UI designing with PYQT, I recommend using QTDesigner initially. Its simple drag and drop.And then the code can be converted to python.
Reply
#6
In Gui Programming, you 'print' to widgets in different ways, depending on the type of widget.
Almost always passed as an argument.
I don't see anywhere in your code the definition of 'screen'

What kind of widget is being used?

There are many widgets that have text attributes, see: http://doc.qt.io/qt-5/examples-widgets.html

usually to display 'text' the attribute is something like text='value', but not always,
Reply
#7
(Dec-19-2017, 12:06 PM)Larz60+ Wrote: In Gui Programming, you 'print' to widgets in different ways, depending on the type of widget.
Almost always passed as an argument.
I don't see anywhere in your code the definition of 'screen'

What kind of widget is being used?

There are many widgets that have text attributes, see: http://doc.qt.io/qt-5/examples-widgets.html

usually to display 'text' the attribute is something like text='value', but not always,

I am not currently using and 'screen' it's all printed to STDOUT. That is why I asked the question, I want to move to either tkinter or PyQt but I don't understand how to post multiple lines to a 'screen' and be able to dynamically update them as things change. All the examples just show a single line of text. I am obviously missing a basic concept here. How do I pass multiple lines of text (as in a for or while loop) to a screen?
Reply
#8
Well, we can't make that decision for you, that's up to the designer, who is you,
and depends on if the text is to be by itself, or with images, if it needs to be edited,
and if so are you going to require such things a cut and paste, copy to end of line,
or to end of text, etc.

Does the text require a response, many responses, etc. Some thing that only requires a
single response or yes/no response does well in a checkbox, whereas something that needs
editing capability should be a textbox, etc.

I suggest that you start by doing a tutorial.

Look at the link I gave you, look for a widget that might do what you need, click on the link next
to that widget, and you will get a complete example of how to construct that widget complete
with code example (in C, python will be very similar)

For python, see: http://zetcode.com/gui/pyqt5/
Reply
#9
Yes, I have done several tutorials on both PyQt and tkinter but neither are clear on how to put more than one line of text on the screen. For brevity sake they all say things like "self.lbl.setText(text)" but that is a single piece of text. How do I send multiple lines of formatted text to a widget?
Reply
#10
I have one more suggestion, and then I'm done.
Take a look at nullege.com and search for sample code
And I don't believe that none of the tutorials were good,
especially zetcode, which has a stellar reputation.
You've got to put something into it to get something out.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] print an image using PyQt micro 4 1,066 Jan-08-2020, 03:50 PM
Last Post: Denni
  Question regarding python pyqt script cibb 2 1,502 Feb-18-2018, 03:03 AM
Last Post: Raures

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020