1

我正在用 Python (PyGTK) 制作一个小程序,它打印出用户输入的一年的日历(公历)。

这是我的代码:

#!/usr/bin/env python

import pygtk, gtk, subprocess
pygtk.require("2.0")

class Base:
    def printing(self, widget):
        text = self.textbox.get_text()
        printingit = "cal -y %s | lpr" % (text)
        process = subprocess.Popen(printingit.split(), stdout=subprocess.PIPE)
        output = process.communicate()[0]   

    def __init__(self):
            self.win = gtk.Window(gtk.WINDOW_TOPLEVEL)
            self.win.set_position(gtk.WIN_POS_CENTER)
            self.win.set_size_request(350, 200)
        self.win.set_resizable(False)
        self.win.set_title("Calendar")
        self.win.connect('destroy',lambda w: gtk.main_quit())

        self.textbox = gtk.Entry()
        self.textbox.set_size_request(70, 30)

        self.lable = gtk.Label("Year:")

        self.button = gtk.Button("Print")
        self.button.set_size_request(60, 45)
        self.button.connect("clicked", self.printing)

        box = gtk.Fixed()
        box.put(self.lable, 160, 25)
        box.put(self.textbox, 140, 40)
        box.put(self.button, 145, 100)

        self.win.add(box)
        self.win.show_all()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    base = Base()
    base.main()

实际打印命令时它不起作用cal -y %s | lpr % (text)。我已经做到了,所以它用它应该得到的最终命令替换了文本框的文本,并且它改变了我想要的cal -y 2015 | lpr。我试着把它放到终端里,它像往常一样工作,这让我很困惑!

我在终端中运行程序,这是我在尝试打印时收到的消息:

Usage: cal [general options] [-hjy] [[month] year]
   cal [general options] [-hj] [-m month] [year]
   ncal [general options] [-bhJjpwySM] [-s country_code] [[month] year]
   ncal [general options] [-bhJeoSM] [year]
General options: [-NC3] [-A months] [-B months]
For debug the highlighting: [-H yyyy-mm-dd] [-d yyyy-mm]

如果有人理解为什么会发生这种情况,我将非常感激!提前谢谢你=D

  • 哈利
4

1 回答 1

1

如果要在命令中使用 shell 语法(管道),则需要将命令作为字符串而Popen不是列表传递给构造函数。你必须使用shell=True

output = subprocess.check_output(printingit, shell=True)

没有它,执行的命令将与以下内容相同:

cal '-y' 'text' '|' 'lpr'

但是当您从文本字段中获取部分输入时,您不应该直接将其传递给 shell。

或者,您可以自己创建管道:

lpr = subprocess.Popen('lpr', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
process = subprocess.Popen(['cal', '-y', text], stdout=lpr.stdin)
output = lpr.communicate()
process.wait()

顺便说一句,cal您可以使用calendar模块来代替使用子进程来调用。cal -y 2012与 相同calendar.calendar(2014),因此您可以将代码替换为:

cal = calendar.calendar(int(text))
process = subprocess.Popen(['lpr'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output = process.communicate(cal)   # cal.encode(locale.getpreferredencoding(False)) for python3
于 2014-04-27T21:35:32.367 回答