Python Forum

Full Version: cmd terminal
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I have the simple program
that I made in Python tkinter

It opens up a cmd window and executes sfc /scannow

What I'm trying to figure out is if I click on the X at the top right corner
I get a ^C how do I prevent this, how do I close the window the proper way


I tried to put it in a try block with a keyboard interruption and that didn't seem to work
I tried several different ways to use kill but I am unclear on how it's supposed to work



def sfc_scannow():
    # sfc / scannow
    if not pyuac.isUserAdmin():
        statusbar.set('Access is denied, you are not an admin')
        root.after(5000, lambda: statusbar.set(''))
        # print('okM')  # for testing
    else:
        sfc_scannow_data()
        


def sfc_scannow_data():
    subprocess.Popen("start /wait sfc /scannow", shell=True)
If you don't display the cmd window it cannot be closed. Do you want the window? If so, why?
(Apr-11-2024, 08:42 PM)deanhystad Wrote: [ -> ]If you don't display the cmd window it cannot be closed. Do you want the window? If so, why?

When I execute the sfc /scannow it does open up the window
then proceeds to check the system out
but if I close the window using the X at the top right corner
I get ^C which if I am correct that represents control C

so how do I properly close this window
But do you need to open the window at all? You can do the scan without opening a cmd window. You do this:
def sfc_scannow_data():
    subprocess.Popen(["sfc", "/scannow"])
If you want to display output from the command you can get that from the Popen call and display it in the tkinter window.
(Apr-12-2024, 11:56 AM)deanhystad Wrote: [ -> ]But do you need to open the window at all? You can do the scan without opening a cmd window. You do this:
def sfc_scannow_data():
    subprocess.Popen(["sfc", "/scannow"])
If you want to display output from the command you can get that from the Popen call and display it in the tkinter window.

Interesting concept I didn't think about that I will certainly give it a try
If one wishes to display the output from the command, how can it be obtained from the Popen call and displayed in a tkinter window?
(Apr-22-2024, 04:30 AM)Rice23 Wrote: [ -> ]If one wishes to display the output from the command, how can it be obtained from the Popen call and displayed in a tkinter window?
You can write
process =  subprocess.run(['command', 'arg', 'arg'], capture_output=True, encoding='utf8')
print(process.stdout)
print(process.stderr)
Then instead of printing the strings process.stdout and process.stderr, you can insert them in a Tkinter Text widget for example.