Jan-26-2019, 03:26 AM
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:
Could you help me, please?
The code:
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).
- Open terminal and run it with command "python + path" --> Result 1 (saved in file.txt)
- 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)
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).