#python #tkinter
#python #tkinter
Вопрос:
У меня есть следующий код в программе TKinter (8.6). Когда я вызываю функцию Run_Report, данные записываются в редактор, как и ожидалось. Однако я бы также хотел, чтобы он распечатал его на моем принтере. Я ввел код, который, я думаю, должен выполнить эту работу, но он возвращает ошибку:
lpr.stdin.write("Population Data" "n".encode())
TypeError: can only concatenate str (not "bytes") to str
Код для функций выглядит следующим образом:
def Run_Report():
import subprocess
lpr = subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE)
sPrinciple=e_SelectInv.get()
Sql=("SELECT Principle as Investment, RptDate as Report_Date, printf('%,.2f',RptVal) as Report_Value, printf('%.2f',ExchRate) as Exchange_Rate, printf('%,d',RptVal*ExchRate) as Total_Value FROM MyInv WHERE Principle = ?"
"order by Id Asc")
conn = sqlite3.connect(r'/home/bushbug/Databases/TrackMyInv')
curs = conn.cursor()
curs.execute(Sql,(sPrinciple,))
col_names = [cn[0] for cn in curs.description]
rows = curs.fetchall()
x = PrettyTable(col_names)
x.align[col_names[0]] = "l"
x.align[col_names[1]] = "r"
x.align[col_names[2]] = "r"
x.align[col_names[3]] = "r"
x.align[col_names[4]] = "r"
x.padding_width = 1
for row in rows:
x.add_row(row)
print (x)
tabstring = x.get_string()
output=open("export.txt","w")
output.write("Population Data" "n")
output.write(tabstring)
lpr.stdin.write("Population Data" "n".encode())
output.close()
conn.close()
Может кто-нибудь, пожалуйста, помочь мне исправить все, что не так в приведенном выше?
Спасибо в ожидании.
Комментарии:
1. Измените
lpr.stdin.write("Population Data" "n".encode())
наlpr.stdin.write(("Population Data" "n").encode())
. На самом деле должно"n"
быть"n"
вместо этого.2. acw1668. Спасибо. Я попробовал это, но он по-прежнему выдает то же сообщение об ошибке. Есть еще мысли?