2014-04-27 4 views
1

저는 사용자가 입력 한 해 동안 달력 (그레고리오)을 인쇄하는 Python (PyGTK)으로 작은 프로그램을 만들고 있습니다. 실제로 명령 cal -y %s | lpr % (text)를 인쇄 할 때 작동하지 않습니다Python을 통해 달력 인쇄. 코드가 작동하지 않습니다 =/

#!/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 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

  • 해리
+1

"작동하지 않는"어떤 맛? 오류 (전체 추적을 제공)? 예기치 못한 결과물 (입력물과 예상되는 결과물 및 실제 결과물 제공)? – jonrsharpe

+0

내가 작동하지 않는다는 말은 내 기본 프린터에서 인쇄하지 않는 것입니다. 내가받은 모든 오류는 게시 된 것이고 출력은 냉동 된 프로그램과 터미널의 오류뿐입니다. 텍스트 필드에 2015를 입력하고 내가 게시 한 출력을 받았습니다. –

+0

왜 절차 코드를 쓸모없는 클래스에 래핑합니까? 자바에 의한 감염인가? –

답변

1

을 주셔서 감사합니다, 당신은 목록과 같이 Popen 생성자에 문자열로 명령을하지 통과해야 . 그리고 당신은 shell=True를 사용해야합니다 : 그없이

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

이 실행 된 명령과 같은 것이다 :

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

그러나 텍스트 필드에서 입력의 일부를 받고있어, 당신 쉘에 직접 전달해서는 안됩니다.

다른 방법으로는 파이프를 직접 만들 수 있습니다

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

을 그건 그렇고, 대신 당신이 calendar 모듈을 사용할 수 있습니다 cal를 호출하는 서브 프로세스를 사용. cal -y 2012calendar.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 
+0

@ J.F.Sebastian - 팁 주셔서 감사합니다. – mata

+0

정말 고마워요! 이것은 정말로 많은 도움을주었습니다. = D –

관련 문제