class Foo(object):
def __init__(self, value):
self.value = value
def __getattr__(self, name):
return lambda: print("'{0}' doesn't exists!".format(name))
foo = Foo(10)
print(foo.value)
foo.some_function()
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import dates
import datetime as dt
fmt = dates.DateFormatter('%H:%M:%S')
fig, ax = plt.subplots()
time_interval = ['19:0:0', '19:1:0', '19:2:0', '19:3:0', '19:4:0']
time_interval = [dt.datetime.strptime(i, "%H:%M:%S") for i in time_interval]
y = np.random.randn(5)
x = np.array([x for x in range(5)])
ax.plot(time_interval, y, "-o")
ax.xaxis.set_major_formatter(fmt)
fig.autofmt_xdate()
plt.show()
from PyQt4 import QtGui
from PyQt4 import QtCore
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__()
layout = QtGui.QVBoxLayout(self)
self.pb = QtGui.QProgressBar()
self.pb.setMaximum(100)
self.pb.setMinimum(0)
self.pb.setValue(0)
layout.addWidget(self.pb)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.on_timer)
self.timer.start(1000)
def on_timer(self):
current_value = self.pb.value() + 1
self.pb.setValue(current_value)
if current_value == 100:
self.timer.stop()
if __name__ == '__main__':
app = QtGui.QApplication([])
w = Widget()
w.show()
app.exec()
from PyQt5 import Qt
class Widget(Qt.QWidget):
def __init__(self):
super().__init__()
layout = Qt.QVBoxLayout(self)
button1 = Qt.QPushButton("Button 1")
button2 = Qt.QPushButton("Button 2")
layout.addWidget(button1)
layout.addWidget(button2)
button1.setStyleSheet("""
QPushButton {
background-color: green;
border-style: outset;
border-width: 2px;
border-radius: 10px;
border-color: beige;
font: bold 14px;
min-width: 10em;
padding: 6px;
}
QPushButton:pressed{
background-color: #4CAF50;
color: white;
border-color: black;
border-style: inset;
}
""")
if __name__ == '__main__':
app = Qt.QApplication([])
w = Widget()
w.show()
app.exec()
from PyQt4 import QtGui, Qt
class MyLabel(QtGui.QLabel):
clicked = Qt.pyqtSignal(str)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def mousePressEvent(self, *args, **kwargs):
self.clicked.emit(self.text())
return QtGui.QLabel.mousePressEvent(self, *args, **kwargs)
class Main(QtGui.QWidget):
def __init__(self):
super().__init__()
layout = QtGui.QVBoxLayout(self)
for i in range(5):
label = MyLabel(self, "Label " + str(i))
label.clicked.connect(self.set_title)
layout.addWidget(label)
self.resize(300, 300)
def set_title(self, label):
self.setWindowTitle(label)
if __name__ == '__main__':
app = Qt.QApplication([])
m = Main()
m.show()
app.exec()
import numpy as np
import matplotlib.pyplot as plt
def func(x, a, b, c):
y = a * x ** 2 + b * x + c
return y
x = np.linspace(0, 5, 300)
y = func(x, 1, 0.2, 2)
plt.figure(1)
plt.subplot(211)
plt.plot(x, y)
plt.subplot(212)
plt.plot(x, y)
plt.xlim(2, 3)
plt.show()
from math import sqrt
array = [1] * 10
for i in range(len(array)):
array[i] *= sqrt(2) ** (i + 1)
print(array)
import numpy as np
array = np.ones(10, dtype='int32')
for i in range(len(array)):
array[i] *= np.sqrt(2) ** (i + 1)
print(array)
import numpy as np
array = np.ones(10, dtype='f4')
for i in range(len(array)):
array[i] *= np.sqrt(2) ** (i + 1)
print(array)
import matplotlib.pyplot as plt
import numpy as np
t = np.linspace(0, np.pi, 314)
y = np.sin(t)
plt.figure(1)
plt.plot(t, y, '-g')
plt.plot(t[100:150:5], y[100:150:5], 'og')
plt.plot(t[::10], y[::10], '*r')
plt.show()
from PyQt5 import Qt
class ExampleWidget(Qt.QWidget):
def __init__(self):
super().__init__()
layout = Qt.QVBoxLayout(self)
layout.addWidget(Qt.QLabel("Label"))
layout.addWidget(Qt.QLineEdit("LineEdit"))
layout.addWidget(Qt.QPushButton("PushButton"))
pb = Qt.QProgressBar()
pb.setValue(66)
layout.addWidget(pb)
if __name__ == '__main__':
app = Qt.QApplication([])
w = ExampleWidget()
w.show()
app.exec()
from PyQt4.QtGui import QWidget, QVBoxLayout, QPushButton, QProgressBar, QApplication
from PyQt4.QtCore import QThread, pyqtSignal
import time
class SomeThread(QThread):
progressed = pyqtSignal(int)
def __init__(self):
super().__init__()
def run(self):
for i in range(1, 11):
self.progressed.emit(i)
time.sleep(0.5)
class App(QWidget):
def __init__(self):
super().__init__()
vbox = QVBoxLayout()
self.pBar = QProgressBar()
self.pBar.setMaximum(10)
vbox.addWidget(self.pBar)
self.button = QPushButton("Start")
vbox.addWidget(self.button)
self.thread = None
self.setLayout(vbox)
self.button.clicked.connect(self.on_button)
def on_button(self):
if not self.thread:
self.thread = SomeThread()
self.thread.progressed.connect(self.on_progress)
self.thread.finished.connect(self.on_finished)
self.thread.start()
def on_progress(self, value):
self.pBar.setValue(value)
def on_finished(self):
self.thread.progressed.disconnect(self.on_progress)
self.thread.finished.disconnect(self.on_finished)
self.thread = None
if __name__ == '__main__':
qApp = QApplication([])
app = App()
app.show()
qApp.exec()
def foo(ekz1, ekz2):
print(ekz1.attr1, ekz2.attr2)
def foo(ekz1, ekz2):
print(getattr(ekz1, "attr1"), getattr(ekz2, "attr2"))
class C1:
def __init__(self, value1):
self.attr1 = value1
class C2:
def __init__(self, value2):
self.attr2 = value2
def func(ekz1, ekz2):
if not hasattr(ekz1, "attr1"):
print("{0} has no attr1".format(type(ekz1)))
return
if not hasattr(ekz2, "attr2"):
print("{0} has no attr2".format(type(ekz2)))
return
print(ekz1.attr1, ekz2.attr2)
inst1 = C1(10)
inst2 = C2(11)
func(inst1, inst2)
func(inst1, [1, 2, 3])
class ORGH_widget(Qt.QWidget):
def __init__(self, parent=None):
super(ORGH_widget, self).__init__(parent)
self.maindialog = ORGH_app(None)
self.layout = Qt.QVBoxLayout(self)
self.layout.addWidget(self.maindialog)
self.maindialog.show()
# self.exec_loop()
def keyPressEvent(self, e):
if e.key() == Qt.Qt.Key_Escape:
self.close()
class ORGH_app(Qt.QMainWindow):
def __init__(self, parent):
super(ORGH_app, self).__init__()
self.form = Ui_Form()
self.form.setupUi(self)
self._connectSlots()
def _connectSlots(self):
self.form.pushButton.clicked.connect(self._slotAddClicked)
def _slotAddClicked(self):
text = self.form.lineEdit.text()
if len(text):
# tvi = Qt.QTableViewItem(self.form.tableView)
# tvi.setText(0, text)
self.form.lineEdit.clear()
if __name__ == "__main__":
app = Qt.QApplication(sys.argv)
main = ORGH_app(None)
main.show()
sys.exit(app.exec())
import csv
lst = [{'value1': 1, 'value2': 10, 'value3': 100, 'value4': 'aaaa'},
{'value1': 2, 'value2': 20, 'value3': 200, 'value4': 'bbbb'},
{'value1': 3, 'value2': 30, 'value3': 300, 'value4': 'cccc'},
{'value1': 4, 'value2': 40, 'value3': 400, 'value4': 'dddd'}]
with open('test.csv', 'w') as csvfile:
writer = csv.DictWriter(csvfile, delimiter=' ', fieldnames=lst[0].keys())
writer.writeheader()
writer.writerows(lst)