0

I'm new to Python programming (Windows) and now I have a problem.

I'm trying to print the following:

def volc():
        os.system(r'Vol C:')
print(volc)

So, that works as it should: it prints C: drives serial number in console.

But the problem is that I want it to be seen in ListBox. If i try:

def volc():
        os.system(r'Vol C:')
Lb1.insert(1, volc)

I get value as "0" in listbox.

What am I doing wrong?

Thanks in advance.


import tkinter as tk
from tkinter import *
import subprocess
from subprocess import check_output
import os

root = tk.Tk()
canvas1 = tk.Canvas(root, width = 300, height = 300)
canvas1.pack()

def volc():
        command = "Vol C:"
        output = check_output(command.split())        
def get_info():
        response = messagebox.askokcancel('Please wait... ', 'Accessing current Hard Drive IDs... Please click "OK". ')
        if response == True:       
            Lb1.insert(6, volc)

button1 = tk.Button (root, text='Get all the Hard Drive IDs ',command=get_info,bg='green',fg='white')
canvas1.create_window(150, 200, window=button1)

root.mainloop()

There is a part of it. The rest is not important.

thuyein
  • 1,684
  • 13
  • 29
  • your volc() is a function, you're inserting a function into the Lb1. Your function volc is supposed to return the value output, then your get_info is supposed to call the function, get output and insert to Lb1. See my modified answer. – thuyein Jul 21 '20 at 00:39

1 Answers1

1

In python, os.system() will execute the command and return you the return code of the command, not the output of the command. os.system python doc

You need to use something like subprocess to get the output of your command. For example

from subprocess import check_output

def volc():
    command = "Vol C:"
    return check_output(command.split())

def get_info():
    ...
    cmd_output = volc()
    Lb1.insert(6, cmd_output)

Refer to this answer for other python versions

Modified answer based on the new info provided by OP

thuyein
  • 1,684
  • 13
  • 29