-1

I am first time to learning PyQt5 and I am puzzled by qlabel The windows 10 cmd "wmic cpu get name" will return current CPU name. (e.g: Name Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz) I want put this result into qlabel. which Public Functions or slots suit for this result? Thanks

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

import xxxx_UI as ui
import subprocess,os,sys

class Main(QMainWindow, ui.Ui_MainWindow):
    def __init__(self):
         super().__init__()
         self.setupUi(self)
         self.Exit_BTN.clicked.connect(self.buttonClicked)
         Processor_content=os.system("wmic cpu get name")
         self.processor_content.settext(self.processor_content)
    def buttonClicked(self):
        window.destroy()

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Main()
    window.show()
    sys.exit(app.exec_())
  • 1
    It should be `self.processor_content.setText(Processor_content)` – Jack Lilhammers Mar 25 '21 at 10:38
  • 1
    Does this answer your question? [Running shell command and capturing the output](https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output) – Jack Lilhammers Mar 25 '21 at 11:06
  • Do not use os.system() since it does not return the result, in the duplicate it indicates how to obtain the output of a command in python – eyllanesc Mar 25 '21 at 19:07

1 Answers1

0

os.system() will not return you the actual output, it will send it to the interpreter's console.

>>Processor_content=os.system("wmic cpu get name")

If you want to get the output into a variable, you should use subprocess.check_output() You can do the following:

>>Processor_content = subprocess.check_output("wmic cpu get name")

But it gives you the byte string as you can see in the following output:

>>Processor_content
b'Name                                      \r\r\nIntel(R) Core(TM) i5-6200U CPU @ 2.30GHz  \r\r\n\r\r\n'

Since, it is a bytestring, you may want to decode it:

>>Processor_content.decode('ascii')
'Name                                      \r\r\nIntel(R) Core(TM) i5-6200U CPU @ 2.30GHz  \r\r\n\r\r\n'

If you want, slightly process this string to make it look better, something like this:

>>Processor_content = Processor_content.decode('ascii').replace('Name', '').strip()
>>Processor_content
'Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz'

Now finally, you can use this string to show in the Qt:

 self.processor_content.setText(Processor_content)
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45