how to create a print dialog box in python3 - printing

I am novice in python and on my way to completing my first desktop application. I have some reports but don't know how to send them to printer with the click of a button

Below is an example I got from PyQT's website. It helped me out quite a bit. You will have to install pip and PyQT5
from the command prompt type:
pip install PyQT5
import sys, os
from PyQt5 import QtCore, QtWidgets, QtPrintSupport
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.setWindowTitle('Document Printer')
self.editor = QtWidgets.QTextEdit(self)
self.editor.textChanged.connect(self.handleTextChanged)
self.buttonOpen = QtWidgets.QPushButton('Open', self)
self.buttonOpen.clicked.connect(self.handleOpen)
self.buttonPrint = QtWidgets.QPushButton('Print', self)
self.buttonPrint.clicked.connect(self.handlePrint)
self.buttonPreview = QtWidgets.QPushButton('Preview', self)
self.buttonPreview.clicked.connect(self.handlePreview)
layout = QtWidgets.QGridLayout(self)
layout.addWidget(self.editor, 0, 0, 1, 3)
layout.addWidget(self.buttonOpen, 1, 0)
layout.addWidget(self.buttonPrint, 1, 1)
layout.addWidget(self.buttonPreview, 1, 2)
self.handleTextChanged()
def handleOpen(self):
path = QtWidgets.QFileDialog.getOpenFileName(
self, 'Open file', '',
'HTML files (*.html);;Text files (*.txt)')[0]
if path:
file = QtCore.QFile(path)
if file.open(QtCore.QIODevice.ReadOnly):
stream = QtCore.QTextStream(file)
text = stream.readAll()
info = QtCore.QFileInfo(path)
if info.completeSuffix() == 'html':
self.editor.setHtml(text)
else:
self.editor.setPlainText(text)
file.close()
def handlePrint(self):
dialog = QtPrintSupport.QPrintDialog()
if dialog.exec_() == QtWidgets.QDialog.Accepted:
self.editor.document().print_(dialog.printer())
def handlePreview(self):
dialog = QtPrintSupport.QPrintPreviewDialog()
dialog.paintRequested.connect(self.editor.print_)
dialog.exec_()
def handleTextChanged(self):
enable = not self.editor.document().isEmpty()
self.buttonPrint.setEnabled(enable)
self.buttonPreview.setEnabled(enable)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())

Related

In pyqt5 browser Createwindows cannot update the url in urlbar

I use Pyqt5 to bulit an easybrowser,in the webpage,most link will built a pop-up window to show new webpages,so I add the code:def createWindow(self, QWebEnginePage_WebWindowType): but en issue was followed:new webpage can not update the url in urlbar,and also the back\forward\stop\reload button doesn't work.it Confuse me so much!
here is my code:
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
import tkinter as tk
import sys
class MainWindow(QMainWindow):
# noinspection PyUnresolvedReferences
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# 设置窗口标题
self.setWindowTitle('My Browser')
# 设置窗口图标
self.setWindowIcon(QIcon('icons/penguin.png'))
# 设置窗口大小900*600
self.resize(900, 600)
self.show()
# 设置浏览器
self.browser = WebEngineView()
url = 'http://www.hao123.com'
# 指定打开界面的 URL
self.browser.setUrl(QUrl(url))
# 添加浏览器到窗口中
self.setCentralWidget(self.browser)
###使用QToolBar创建导航栏,并使用QAction创建按钮
# 添加导航栏
navigation_bar = QToolBar('Navigation')
# 设定图标的大小
navigation_bar.setIconSize(QSize(16, 16))
#添加导航栏到窗口中
self.addToolBar(navigation_bar)
#QAction类提供了抽象的用户界面action,这些action可以被放置在窗口部件中
# 添加前进、后退、停止加载和刷新的按钮
back_button = QAction(QIcon('icons/back.png'), 'Back', self)
next_button = QAction(QIcon('icons/next.png'), 'Forward', self)
stop_button = QAction(QIcon('icons/cross.png'), 'stop', self)
reload_button = QAction(QIcon('icons/renew.png'), 'reload', self)
back_button.triggered.connect(self.browser.back)
next_button.triggered.connect(self.browser.forward)
stop_button.triggered.connect(self.browser.stop)
reload_button.triggered.connect(self.browser.reload)
# 将按钮添加到导航栏上
navigation_bar.addAction(back_button)
navigation_bar.addAction(next_button)
navigation_bar.addAction(stop_button)
navigation_bar.addAction(reload_button)
#添加URL地址栏
self.urlbar = QLineEdit()
# 让地址栏能响应回车按键信号
self.urlbar.returnPressed.connect(self.navigate_to_url)
navigation_bar.addSeparator()
navigation_bar.addWidget(self.urlbar)
#让浏览器相应url地址的变化
self.browser.urlChanged.connect(self.renew_urlbar)
def navigate_to_url(self):
q = QUrl(self.urlbar.text())
if q.scheme() == '':
q.setScheme('http')
self.browser.setUrl(q)
def renew_urlbar(self, q):
# # 将当前网页的链接更新到地址栏
self.urlbar.setText(q.toString())
self.urlbar.setCursorPosition(0)
class WebEngineView(QWebEngineView):
windowList = []
# 重写createwindow()
def createWindow(self, QWebEnginePage_WebWindowType):
new_webview = WebEngineView()
new_window = MainWindow()
new_window.setCentralWidget(new_webview)
new_window.show()
self.windowList.append(new_window) # 注:没有这句会崩溃!!!
return new_webview
# 创建应用
app = QApplication(sys.argv)
if __name__ == "__main__":
app = QApplication.instance()
# 创建主窗口
window=MainWindow()
# 显示窗口
window.show()
# 运行应用,并监听事件
app.exec_()
You are creating the new window correctly, but you also set a new webview for that window while it already has one.
Just return its existing browser webview instead of creating a new one.
class WebEngineView(QWebEngineView):
windowList = []
def createWindow(self, QWebEnginePage_WebWindowType):
new_window = MainWindow()
new_window.show()
self.windowList.append(new_window)
return new_window.browser

How can I use Chinese in Alpine headless chrome?

I am using headless chrome based on alpine:3.7 image under docker. However, when I take a screenshot using Chrome DevTools Protocol, I found alpine cannot show languages other than English like the screenshot below.
I have tried to copy font to docker image
COPY NotoSansCJKsc-Regular.otf /usr/share/fonts/
and tried to install font in alpine source:
RUN apk add --no-cache bash \
ttf-ubuntu-font-family \
font-adobe-100dpi \
font-noto \
ttf-dejavu \
But all methods failed. So my question is is Alpine support Chinese? And How can I use it using Docker?
python code to take the screenshot:
import threading
import pychrome
import time
import base64
class EventHandler(object):
screen_lock = threading.Lock()
def __init__(self, browser, tab):
self.browser = browser
self.tab = tab
self.start_frame = None
self.html_content = None
self.is_first_request = True
def frame_start_loading(self, frameId):
if not self.start_frame:
self.start_frame = frameId
def request_intercepted(self, interceptionId, request, **kwargs):
if self.is_first_request:
self.is_first_request = False
headers = request.get('headers', {})
self.tab.Network.continueInterceptedRequest(
interceptionId=interceptionId,
headers=headers,
method='POST',
postData="hello post data: %s" % time.time()
)
else:
self.tab.Network.continueInterceptedRequest(
interceptionId=interceptionId
)
def frame_stop_loading(self, frameId):
if self.start_frame == frameId:
self.tab.Page.stopLoading()
result = self.tab.Runtime.evaluate(expression="document.documentElement.outerHTML")
self.html_content = result.get('result', {}).get('value', "")
print self.html_content
self.tab.stop()
def close_all_tabs(browser):
if len(browser.list_tab()) == 0:
return
for tab in browser.list_tab():
try:
tab.stop()
except pychrome.RuntimeException:
pass
browser.close_tab(tab)
assert len(browser.list_tab()) == 0
if __name__ == '__main__':
# create a browser instance
browser = pychrome.Browser(url="http://127.0.0.1:9222")
print browser.version()
# create a tab
tab = browser.new_tab()
# register callback
def request_will_be_sent(**kwargs):
print("[start] start loading: %s" % kwargs.get('request').get('url'))
tab.set_listener("Network.requestWillBeSent", request_will_be_sent)
def received_callback(**kwargs):
print "[received] response type %s" % kwargs.get('type', '')
resp = kwargs.get('response', {})
resp_status = resp.get('status', '')
resp_headers = resp.get('headersText')
print "response status %s %s" % (resp_status, resp_headers)
tab.set_listener("Network.responseReceived", received_callback)
def frame_stop_loading():
print "frame stop loading"
tab.set_listener("Page.frameStoppedLoading", frame_stop_loading)
def loading_finished(**kwargs):
print "[loading finished]"
# when HTTP request has finished loading
tab.set_listener("Network.loadingFinished", loading_finished)
# start the tab
tab.start()
net = tab.call_method("Network.enable")
navi = tab.call_method("Page.navigate", url="https://www.douban.com", _timeout=5)
tab.wait(5)
screen_data = tab.call_method("Page.captureScreenshot")
image_data = screen_data.get('data', '')
with open("image.png", "wb") as file:
file.write(image_data.decode('base64'))
html = tab.Runtime.evaluate(expression="document.documentElement.outerHTML")
print html['result']['value']
all_cookies = tab.Network.getAllCookies()
print all_cookies
tab.stop()
browser.close_tab(tab)
Install wqy-zenhei package at testing channel will fix it.
echo #edge http://nl.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories && apk add wqy-zenhei#edge

wxPython: write SQL command results to outputbox

I'm trying to get back into Python and I'm once again stuck with this problem I've had before of making objects accessible to one another. In this simple example I am displaying a panel with a button and a text box. Clicking on the text box calls a function which queries a database and returns a cursor with the retrieved data. I need to make it so that either the LookupSQL function or the ShowClientData function can write this output, in a loop, to the Text box. The TextBox (outputBox) is unknown to any other functions currently. How do I make it so that the other functions know what it is?
import wx
import pypyodbc
conn = pypyodbc.connect(driver='{SQL Server}', server='.', database='TheDB', uid='sa', pwd='Pass')
class Audit(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
panel = wx.Panel(self)
hbox = wx.BoxSizer()
sizer = wx.GridSizer(6,1,2,2)
btn1 = wx.Button(panel, label='Clients')
outputBox = wx.TextCtrl(panel, -1, style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
sizer.AddMany([btn1, btn2, btn3, btn4, btn5, btn6])
hbox.Add(sizer, 0, wx.ALL, 15)
hbox.Add(outputBox, 1, wx.EXPAND)
panel.SetSizer(hbox)
btn1.Bind(wx.EVT_BUTTON, self.ShowClientData)
self.SetSize((800, 600))
self.SetTitle('Audit View')
self.Centre()
self.Show(True)
def ShowClientData(self, event):
SQL = 'select * from V_UpdatedClient'
recursor = lookupSQL(SQL)
for row in recursor:
rChange = row[0]
rItemType = row[1]
rPK = row[2]
rItemCode = row[3]
rFieldName = row[4]
rOldValue = row[5]
rNewValue = row[6]
rUpdateDate = row[7]
rUserName = row[8]
print('%s %s %s %s %s %s %s %s %s' % (rChange, rItemType, rPK, rItemCode, rFieldName, rOldValue, rNewValue, rUpdateDate, rUserName))
def lookupSQL(SQLString):
cursor = conn.cursor()
cursor.execute(SQLString)
return cursor
cursor.close()
def main():
ex = wx.App()
Audit(None)
ex.MainLoop()
if __name__ == '__main__':
main()
What you are looking for is called data attributes.
self.outputBox = wx.TextCtrl(panel, -1, style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
And then within ShowClientData you can write
self.outputBox.AppendText("some text")
As long as you have that self reference, you can access its attributes.
Edit:
When you do the above change, you can't refer to the text box by just outputBox anymore, you should instead access it via self:
hbox.Add(self.outputBox, 1, wx.EXPAND)
Declaring it as globally is very bad!

How to save Text of several QtextEdits within python PYQt4?

Could you please help me in solving this issue as i am a python PyQt beginner ...i have created a simple application with python PyQt4 for a simple math calculations and having the result in textEdit2 and textEdit3 every thing went fine when i click on calculate push button, values appear in the above textedits...what i want is to save the results on the same text edits so that next time when i open the application and open the data file i find every thing in place. I created the save and open actions...but it saves all the results in the same text edit..
i am attaching my code and also the main window with results that needed to be saved and another shot after saving and opening the application and having all the results in one textedit
enter code here
import sys
from PyQt4 import QtCore, QtGui, uic
import os #os used to interface with windows
import datetime as dt
import pickle
qtCreatorFile = "save.ui" # Enter ui file here
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class MyApp(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
super (MyApp,self).__init__()
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.initUI()
self.calc.clicked.connect(self.Bss)
def Bss (self):
x = float(self.textEdit.toPlainText())
y = float(self.textEdit1.toPlainText())
z= x+y
w= x**2
z_string = str(z) #convert z value to a string then add this string to textEdit and set as Text
self.textEdit2.setText(z_string)
w_string = str(w)
self.textEdit3.setText(w_string)
def initUI(self): #QAction class insert New,action save in the main menue bar
newAction = QtGui.QAction('New', self)
newAction.setShortcut('Ctrl+N')
newAction.setStatusTip('Create new file')
newAction.triggered.connect(self.newFile)
saveAction = QtGui.QAction('Save as', self)
saveAction.setShortcut('Ctrl+S')
saveAction.setStatusTip('Save current file')
saveAction.triggered.connect(self.saveFile)
openAction = QtGui.QAction('Open', self)
openAction.setShortcut('Ctrl+O')
openAction.setStatusTip('Open a file')
openAction.triggered.connect(self.openFile)
closeAction = QtGui.QAction('Close', self)
closeAction.setShortcut('Ctrl+Q')
closeAction.setStatusTip('Close Notepad')
closeAction.triggered.connect(self.close)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(newAction)
fileMenu.addAction(saveAction)
fileMenu.addAction(openAction)
fileMenu.addAction(closeAction)
self.show()
self.statusBar()
def newFile(self): #create a new function and remove all values in the QTEXTedit
self.textEdit.clear()
self.textEdit1.clear()
self.textEdit2.clear()
self.textEdit3.clear()
def openFile(self):
self.text = QtGui.QTextEdit(self)
filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', os.getenv('HOME'))
f = open(filename, 'r')
filedata = f.read()
self.textEdit1.setText(filedata)
self.textEdit2.setText(filedata)
self.textEdit3.setText(filedata)
f.close()
self.show()
def saveFile(self): # saving argument values in a filedata
self.text = QtGui.QTextEdit(self)
filename = QtGui.QFileDialog.getSaveFileName(self, 'Save File', os.getenv('HOME'))
f = open(filename, 'w')
filedata = self.textEdit1.toPlainText()
f.write(filedata)
filedata = self.textEdit2.toPlainText()
f.write(filedata)
filedata = self.textEdit3.toPlainText()
f.write(filedata)
f.close()
def closeEvent(self, event):
reply = QtGui.QMessageBox.question(self, 'Message',
"Are you sure to quit?", QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
if I understand your question properly, since you are writing textEdit values to a file in order, you'll need to read that values line by line when opening the file, instead of reading the whole file content using read() method. so the openFile() method should be something like this:
def openFile(self):
self.text = QtGui.QTextEdit(self)
filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', os.getenv('HOME'))
count = 0
with open(filename,'r') as f:
for line in f:
count = count + 1
if count == 1 : self.textEdit1.setText(line)
elif count == 2 : self.textEdit2.setText(line)
elif count == 3 : self.textEdit3.setText(line)
else: break
self.show()
hope it helps!
UPDATE:
also change saveFile() method like below:
def saveFile(self): # saving argument values in a filedata
self.text = QtGui.QTextEdit(self)
filename = QtGui.QFileDialog.getSaveFileName(self, 'Save File', os.getenv('HOME'))
f = open(filename, 'w')
filedata = self.textEdit1.toPlainText()
filedata = str(filedata)+"\n"
f.write(filedata)
filedata = self.textEdit2.toPlainText()
filedata = str(filedata)+"\n"
f.write(filedata)
filedata = self.textEdit3.toPlainText()
filedata = str(filedata)+"\n"
f.write(filedata)
f.close()

wxPython: How to bind the button?

I just started with wxPython and have problems with the binding.
Usually the examples I find to bind a button event are done withself.Bind(wx.EVT_BUTTON, self.OnWhatEverFunction, button). I have a frame with a panel inside and a button and what ever I tried, the outcome is always a flashing frame that appears in a split second and that's it. Since the code isn't that much I attach it here and hope one of you can show me the way out of my little problem.
Thanks in advance,
Thomas
#!/usr/bin/python
import wx
class CPFSFrame(wx.Frame):
def __init__(self, parent, title):
super(CPFSFrame, self).__init__(parent, title=title,
style = wx.BORDER | wx.CAPTION | wx.SYSTEM_MENU | wx.CLOSE_BOX,
size=(350, 200))
panel = wx.Panel(self, -1)
pNumberLabel = wx.StaticText(panel, -1, 'Project number: ')
pNumberText = wx.TextCtrl(panel, -1, '', size=(175, -1))
pNumberText.SetInsertionPoint(0)
pNameLabel = wx.StaticText(panel, -1, 'Project name: ')
pNameText = wx.TextCtrl(panel, -1, '', size=(175, -1))
pNameText.SetInsertionPoint(0)
pButton = wx.Button(panel, label='Create')
pButton.Bind(wx.EVT_BUTTON, self.OnCreate)
sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
sizer.AddMany([pNumberLabel, pNumberText, pNameLabel, pNameText, pButton])
panel.SetSizer(sizer)
statusBar = self.CreateStatusBar()
def OnCreate(self, evt):
self.Close(True)
self.Centre()
self.Show()
if __name__ == '__main__':
app = wx.App()
CPFSFrame(None, title='Create New Project Folder Structure')
app.MainLoop()
You need to put the "self.Show()" method in the init section of the code. At first I thought OnCreate() was running and if it was, then it would just close the frame immediately so you wouldn't see anything. I would call it OnClose instead. Here's a working example:
import wx
class CPFSFrame(wx.Frame):
def __init__(self, parent, title):
super(CPFSFrame, self).__init__(parent, title=title,
style = wx.BORDER | wx.CAPTION | wx.SYSTEM_MENU | wx.CLOSE_BOX,
size=(350, 200))
panel = wx.Panel(self, -1)
pNumberLabel = wx.StaticText(panel, -1, 'Project number: ')
pNumberText = wx.TextCtrl(panel, -1, '', size=(175, -1))
pNumberText.SetInsertionPoint(0)
pNameLabel = wx.StaticText(panel, -1, 'Project name: ')
pNameText = wx.TextCtrl(panel, -1, '', size=(175, -1))
pNameText.SetInsertionPoint(0)
pButton = wx.Button(panel, label='Create')
pButton.Bind(wx.EVT_BUTTON, self.OnClose)
sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
sizer.AddMany([pNumberLabel, pNumberText, pNameLabel, pNameText, pButton])
panel.SetSizer(sizer)
statusBar = self.CreateStatusBar()
self.Show()
def OnClose(self, evt):
self.Close(True)
if __name__ == '__main__':
app = wx.App()
CPFSFrame(None, title='Create New Project Folder Structure')
app.MainLoop()

Resources