How to create a console in GUI app using pyqt?



  • Does anyone have an idea?
    I used QTextEdit and QLineEdit to create a console.
    I used eval to run custom command.
    Is it the correct method to do it?


  • Qt Champions 2018

    @Moeka_Chan said in How to create a console in GUI app using pyqt?:

    Is it the correct method to do it?

    Depends on what you mean with "console". Do you want to embed the console of your system (which OS is it?) into your app?



  • @jsulm My OS is windows 10. I want to embed python console. Also, the console can run my custom command. I don't find any tutorials related it from Google... It should be like console in Blender.



  • Are you saying what you want to do is write a fully functionally and rich GUI interface like you find with Blender's Console application? If yes then you are not wanting to create a "console" you are wanting to create a fully functional and rich GUI interface APPLICATION and glancing at the Blender APPLICATION which they call a Console which is most likely just short for "Console Application" you are going to want to start with a QWindow and build from there and there are lots of tutorials on all the aspects that are shown within Blender you just have to have a better understanding of what you are actually trying to do so you can ask more precise questions about what help you need because no one is going to write a fully functional rich GUI interface application for you for free or give you sample code for something that resembles Blender. Note please take no offense as none was intended just making sure that I am crossing all my T's and dotting all my I's so that we can be sure to be talking about the same thing.

    Addendum: Having looked a bit more at Blender I see it has something it calls a console application but all this application seems to do is interact with the terminal window from in which it was launched aka -- from what I understood -- it just uses whatever terminal (command prompt) window you have already opened via some other bit of software. Is this what you are referring too? If yes then QTextEdit and QLineEdit would be a means of implementing something like that sort-of I mean you are not interacting with the terminal window but more just creating one of your own.


  • Qt Champions 2018



  • @Denni-0 I want to interact with the terminal window. I am a Blender user. I can create a customized class or function to run in Blender. I think I need to create a widget for a console to be added in my App. There is no console widget, but PyqtGraph has consolewidget. I also checked console application, script window, command window, console interface, dev console. Because they are shown in App GUI, I don't know what correct word to search...

    One more question, some games have their own console. For example, players can change a mouse cursor size or select game server in Dota 2 console. I am not sure whether I can create some variables, like a = 2, in Dota 2 console. (I didn't test it before). Do you have any ideas on how to create this console?



  • @jsulm No. I will check it later.



  • @Moeka_Chan okay let me see if I can get this straight you are wanting to:

    Open Blender in Console mode in a Terminal (Command Prompt) Window
    Create a program that you can use to interact with Blender within this Terminal Window
    --or--
    Create your own program that works like the Blender in Console mode and interact with a pre-existing Terminal (Command Prompt) Window
    --or--
    Create your own Terminal (Command Prompt) Window application that will allow the Blender in Console mode to operate
    --or--
    Create your own Terminal (Command Prompt) Window application for your own use

    The first two would require you to create your own Python (only) program that interacts with a pre-existing Terminal (Command Prompt) Window

    The last two would require you to create your own emulation of a Terminal (Command Prompt) Window which would be no easy endeavor and would be re-inventing an already very well designed wheel but it could be done but again just not very easily

    If what you are trying to do is not one of the 4 things I listed please try to describe in more detail what you are attempting to do



  • @Denni-0
    0_1563514628781_QQ图片20190719013657.png
    0_1563514548949_QQ图片20190719013514.png
    I want to create my own program that has a console. It is like Command Window in Matlab or Console in Blender, which is shown in the red rectangle in the images above.



  • okay so to answer the original questions reworded slightly :

    @Moeka_Chan said in How to create a console in GUI app using pyqt?:

    If I used QTextEdit and QLineEdit would that be the correct method to simulate a
    console (terminal window) like object that I could put into my main window application

    Well it would be "A" method to simulate this but technically there is no "correct method" just whatever method you are choosing -- if you are wanting to exactly render your own version of one these terminal-like windows then no it would not be the correct method but then I would have to ask why do you need it to be exact -- a close approximation should be sufficient for most things

    Putting the two objects QTextEdit and QLineEdit within a QVBoxLayout in that order and then place code within the QLineEdit event to handle the entry of data such that it copies that data to the last line in the QTextEdit box while erasing it from the QLineEdit box would do nicely I would think -- aesthetically making the QLineEdit and QTextEdit objects the same color with same colored text and making the border between them seem non-existent would enhance the visual affect that they are one object but would not functionally be necessary.



  • @Denni-0 Most of the software I used has a scripting section. For example, Motionbuilder, Maya, Blender, Unity, Matlab... Users can write their scripts to run there. Currently, I am not going to add this feature in my program. Usually, the scripting section includes TextEdit for scripting and a console to run some internal functions or execute the scripts. I am going to create a console in my program, but I have no blueprint on it. I found a tutorial showing how to create a dev console on a webpage, and it used TextEdit and LineEdit to achieve it. However, the console in the tutorial only works for customized command. It can't run any Python, Java method.....



  • Well that would be mutable -- as whatever is entered could be simply executed -- however this is going to give an extreme amount of power to the user of your application and basically allow them to do anything at all that they so choose which may not be all that advantageous... still all you would have to do is take said "script" and wrap it and then call a python or command line execute function to run it. I have done this before and yeah bare bones all you need is the QLineEdit (or QTextEdit) for inputting the script and a means to view what has been entered and/or the results which a QTextEdit is sufficient for. The QLineEdit versus a QTextEdit for script entry would simply be the difference between a single line editor and a multi-line editor --- of course this does not take into account any error checking for the script it will simply run or crash -- the script checking would have to be handled prior to running it.



  • @Denni-0 wrap it and then call a python or command line execute function to run it
    Does it use methods like eval, subprocess?



  • Well for Python it would use something along the lines of the following:
    https://linuxhandbook.com/execute-shell-command-python/

    Now there are a number of ways to implement executing command line code from within a program but none of them have anything to do with implementing a GUI using pyqt5. Again all pyqt5 would be used for is creating a GUI that is used to get the command from the user. The code would then process that in the back-end based on various criterion such as Language being used, OS being talked too, and other items that might be involved in executing such commands on the hardware your program is running on. I am sure if you google this topic in regards to the language you want to use for your back-end such as python (since you might be using pyqt5 as your front-end) I am sure you can find more topics like the one I shared that will help you fine-tune what you are doing.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.