Skip to content

Commit 6a26b0d

Browse files
committed
update
1 parent 290f4cd commit 6a26b0d

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

pyqt5_lesson14.py

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
#! /usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
###############################################################
4+
# kenwaldek GPL-license
5+
6+
# Title: PyQt5 lesson 14 Version: 1.0
7+
# Date: 09-01-17 Language: python3
8+
# Description: pyqt5 gui and opening files
9+
# pythonprogramming.net from PyQt4 to PyQt5
10+
###############################################################
11+
# do something
12+
13+
14+
import sys
15+
from PyQt5.QtCore import QCoreApplication, Qt
16+
from PyQt5.QtGui import QIcon, QColor
17+
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton, QAction, QMessageBox
18+
from PyQt5.QtWidgets import QCalendarWidget, QFontDialog, QColorDialog, QTextEdit, QFileDialog
19+
from PyQt5.QtWidgets import QCheckBox, QProgressBar, QComboBox, QLabel, QStyleFactory, QLineEdit, QInputDialog
20+
21+
22+
23+
class window(QMainWindow):
24+
25+
def __init__(self):
26+
super(window, self).__init__()
27+
self.setGeometry(50, 50, 800, 500)
28+
self.setWindowTitle('pyqt5 Tut')
29+
self.setWindowIcon(QIcon('pic.png'))
30+
31+
extractAction = QAction('&Get to the choppah', self)
32+
extractAction.setShortcut('Ctrl+Q')
33+
extractAction.setStatusTip('leave the app')
34+
extractAction.triggered.connect(self.close_application)
35+
36+
openEditor = QAction('&Editor', self)
37+
openEditor.setShortcut('Ctrl+E')
38+
openEditor.setStatusTip('Open Editor')
39+
openEditor.triggered.connect(self.editor)
40+
41+
openFile = QAction('&Open File', self)
42+
openFile.setShortcut('Ctrl+O')
43+
openFile.setStatusTip('Open File')
44+
openFile.triggered.connect(self.file_open)
45+
46+
self.statusBar()
47+
48+
mainMenu = self.menuBar()
49+
fileMenu = mainMenu.addMenu('&File')
50+
fileMenu.addAction(extractAction)
51+
52+
fileMenu.addAction(openFile)
53+
54+
editorMenu = mainMenu.addMenu('&Editor')
55+
editorMenu.addAction(openEditor)
56+
57+
extractAction = QAction(QIcon('pic.png'), 'flee the scene', self)
58+
extractAction.triggered.connect(self.close_application)
59+
self.toolBar = self.addToolBar('extraction')
60+
self.toolBar.addAction(extractAction)
61+
62+
fontChoice = QAction('Font', self)
63+
fontChoice.triggered.connect(self.font_choice)
64+
# self.toolBar = self.addToolBar('Font')
65+
self.toolBar.addAction(fontChoice)
66+
67+
cal = QCalendarWidget(self)
68+
cal.move(500, 200)
69+
cal.resize(200, 200)
70+
71+
self.home()
72+
73+
def editor(self):
74+
self.textEdit = QTextEdit()
75+
self.setCentralWidget(self.textEdit)
76+
77+
78+
79+
def file_open(self):
80+
# need to make name an tupple otherwise i had an error and app crashed
81+
name, _ = QFileDialog.getOpenFileName(self, 'Open File', options=QFileDialog.DontUseNativeDialog)
82+
print('tot na dialog gelukt')
83+
file = open(name, 'r')
84+
print('na het inlezen gelukt')
85+
self.editor()
86+
87+
with file:
88+
text = file.read()
89+
self.textEdit.setText(text)
90+
91+
##End of code problem
92+
93+
def color_picker(self):
94+
color = QColorDialog.getColor()
95+
self.styleChoice.setStyleSheet('QWidget{background-color: %s}' % color.name())
96+
97+
def font_choice(self):
98+
font, valid = QFontDialog.getFont()
99+
if valid:
100+
self.styleChoice.setFont(font)
101+
102+
def home(self):
103+
btn = QPushButton('quit', self)
104+
btn.clicked.connect(self.close_application)
105+
btn.resize(btn.sizeHint())
106+
btn.move(0, 100)
107+
108+
checkBox = QCheckBox('Enlarge window', self)
109+
# checkBox.toggle() # if you want to be checked in in the begin
110+
checkBox.move(0, 50)
111+
checkBox.stateChanged.connect(self.enlarge_window)
112+
113+
self.progress = QProgressBar(self)
114+
self.progress.setGeometry(200, 80, 250, 20)
115+
116+
self.btn = QPushButton('download', self)
117+
self.btn.move(200, 120)
118+
self.btn.clicked.connect(self.download)
119+
120+
self.styleChoice = QLabel('Windows', self)
121+
comboBox = QComboBox(self)
122+
comboBox.addItem('motif')
123+
comboBox.addItem('Windows')
124+
comboBox.addItem('cde')
125+
comboBox.addItem('Plastique')
126+
comboBox.addItem('Cleanlooks')
127+
comboBox.addItem('windowsvista')
128+
129+
comboBox.move(25, 250)
130+
self.styleChoice.move(25, 150)
131+
comboBox.activated[str].connect(self.style_choice)
132+
133+
color = QColor(0,0,0)
134+
fontColer = QAction('font bg color', self)
135+
fontColer.triggered.connect(self.color_picker)
136+
self.toolBar.addAction(fontColer)
137+
138+
self.show()
139+
140+
def style_choice(self, text):
141+
self.styleChoice.setText(text)
142+
QApplication.setStyle(QStyleFactory.create(text))
143+
144+
def download(self):
145+
self.completed = 0
146+
147+
while self.completed < 100:
148+
self.completed += 0.0001
149+
self.progress.setValue(self.completed)
150+
151+
152+
def enlarge_window(self, state):
153+
if state == Qt.Checked:
154+
self.setGeometry(50, 50, 1000, 600)
155+
else:
156+
self.setGeometry(50, 50 , 500, 300)
157+
158+
159+
def close_application(self):
160+
161+
choice = QMessageBox.question(self, 'Message',
162+
"Are you sure to quit?", QMessageBox.Yes |
163+
QMessageBox.No, QMessageBox.No)
164+
165+
if choice == QMessageBox.Yes:
166+
print('quit application')
167+
sys.exit()
168+
else:
169+
pass
170+
171+
172+
if __name__ == "__main__": # had to add this otherwise app crashed
173+
174+
def run():
175+
app = QApplication(sys.argv)
176+
Gui = window()
177+
sys.exit(app.exec_())
178+
179+
run()

0 commit comments

Comments
 (0)