Python - 读取二维码,在Tkinter中输出不正确

时间:2013-04-12 11:40:05

标签: python tkinter

我正在使用libdmtx通过以下代码从png文件中读取二维码:

#!/usr/bin/python
import os
import Tkinter
from Tkinter import *
from tkFileDialog import askopenfilename

top = Tkinter.Tk()
top.geometry("200x200")
content = StringVar()
label = Message( top, textvariable=content, width='180' )
content.set ("Choose file to read 2D code")
label.pack()

def selector():
   filename = askopenfilename() 
   cmd = "dmtxread -n %s" % (filename)
   res = Text(top)
   res.insert (INSERT, os.system(cmd))
   res.pack()

B = Tkinter.Button(top, text ="Choode file", command = selector)
B.pack()

top.mainloop()

一切正常,但在GUI中我无法获得完整的输出。只有0,但在控制台我得到2D代码。 我该怎么做才能在GUI中获得完整的输出?

1 个答案:

答案 0 :(得分:2)

那是因为os.system没有返回你执行的命令的stdout,它只返回退出状态,在这种情况下是0

您应该使用subprocess模块,如下所示:

import subprocess

def selector():
    filename = askopenfilename() 
    p = subprocess.Popen(["dmtxread", "-n", filename], stdout=subprocess.PIPE)
    stdout, stderr = p.communicate()
    res.insert(INSERT, stdout)
    res.pack()