Python Forum
Same code - Different results
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Same code - Different results
#1
Hi everyone! I'm new in Linux/Python and I'm facing something strange. I have been obtaining different results executing the same piece of code. To summarize:
  1. Open terminal and run it with command "python + path" --> Result 1 (saved in file.txt)
  2. Configure script to run as program (right click over .py file/properties/permissions and check "Allow executing file as program") and double-clicking to execute--> Result 2 (saved in file.txt too)
Comparing the results, we can see that they are totally different.
Could you help me, please?

The code:

#!/usr/bin/env python3
from tkinter import *
from multiprocessing import *
import subprocess

CommandQueue = Queue()
l = Label

def Commands(MyQueue):
    cmd = MyQueue.get()
    zz = subprocess.Popen(['/bin/sh',"-c",cmd],bufsize=-1,universal_newlines=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=False)
    MyQueue.put(zz.communicate()[0])

class MainWindow:
    def __init__(self, master):
        
        global CommandQueue,l  
        MainProcess = Process(target=Commands, args=(CommandQueue,))
        MainProcess.name = "MyProcess"
        MainProcess.start()

        d = Button(None, text="GetData", width=2, height=2, command=self.GetCMD)
        d.place(x=0,y=0)  

        l = Label(master,text="asasasas")
        l.place(x=50,y=50)  
    
    def GetCMD(self):
        global CommandQueue,l
        CommandQueue.put("printenv")
        l.config(text=CommandQueue.get()) 
        file = open("./file.txt","w")
        file.write(l.cget("text"))
        file.close        


root = Tk()
MW = MainWindow(root)
root.height = 300
root.width = 300
root.mainloop()
P.S:
Python version - 3.5
O.S - Ubuntu 16.04
Goal - Create a graphical interface to execute shell/terminal command (without window), receive output and present it. The interface must be executed using "double-click" (just for user convenience).
Reply


Forum Jump:

User Panel Messages

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