def main(): app = QApplication(sys.argv) QW = QWidget() zhu = Ui_Form() zhu.setupUi(QW) QW.show() sys.exit(app.exec_())
def show_and_wait_for_active(self, widget: QWidget, timeout: int = 5000) -> None: """Wait for active widget""" widget.show() if self.is_interactive(): # noinspection PyTypeChecker,PyCallByClass QTest.qWaitForWindowActive(widget, timeout)
def window(): app = QApplication(sys.argv) w = QWidget() # b = QLabel(w) # b.setText("Hello World!") w.setGeometry(100,100,200,50) # b.move(50,20) w.setWindowTitle("test") w.show() painter = QPainter() painter.setRenderHint(QPainter.Antialiasing) painter.setBackground(QColor(0, 200, 0)) painter.setBrush(QColor(0, 0, 200)) # painter.setPen(QPen(QColor(0, 0, 200), 2, Qt.SolidLine)) painter.drawEllipse(40, 40, 40, 40) # painter.setBrush(Qt.NoBrush) sys.exit(app.exec_())
class WeirdViewClient(QMainWindow): """ Weird view client initializer. """ def __init__(self): super().__init__() self.device_selector_window = QMainWindow() self.device_selector = DeviceSelector(self.device_selector_window) self.device_selector.device_selection_signal.connect(self.device_open) self.device_selector_window.show() """ Weird view device open callback. """ def device_open(self, name, address): # Close the old device selector window. self.device_selector_window.close() self.plugin_window = QWidget() self.plugin = PluginWindow(self.plugin_window, name, address) self.plugin_window.show()
def show(self): if self.current_results: self.layout() QWidget.show(self) self.raise_()
def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.comboBox_2.setItemText(0, _translate("Form", "null")) self.comboBox_2.setItemText(1, _translate("Form", "冒泡排序")) self.comboBox_2.setItemText(2, _translate("Form", "快速排序")) self.comboBox_2.setItemText(3, _translate("Form", "归并排序")) self.comboBox_2.setItemText(4, _translate("Form", "选择排序")) self.comboBox_2.setItemText(5, _translate("Form", "希尔排序")) self.comboBox_2.setItemText(6, _translate("Form", "插入排序")) self.comboBox_2.setItemText(7, _translate("Form", "计数排序")) self.comboBox_2.setItemText(8, _translate("Form", "基数排序")) self.comboBox_2.setItemText(9, _translate("Form", "堆排序")) self.comboBox_2.setItemText(10, _translate("Form", "桶排序")) self.pushButton_4.setText(_translate("Form", "下一步")) self.pushButton.setText(_translate("Form", "提交")) self.pushButton_2.setText(_translate("Form", "开始测试")) self.pushButton_3.setText(_translate("Form", "上一步")) from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.Qt import QApplication, QWidget, QMovie import sys if __name__ == '__main__': app = QApplication(sys.argv) tem = QWidget() a = Ui_Form() test = a.setupUi(tem) tem.show() app.exec()
self.animation.setEndValue(size) QMetaObject.invokeMethod(self.animation, 'start', Qt.QueuedConnection) def stop_animation(self): self.animation.stop() self.animation_finished() def paintEvent(self, ev): size = self._icon_size if self._icon_size > 10 else self.iconSize().width() p = QPainter(self) opt = QStyleOptionToolButton() self.initStyleOption(opt) s = self.style() opt.iconSize = QSize(size, size) s.drawComplexControl(s.CC_ToolButton, opt, p, self) if __name__ == '__main__': from PyQt5.Qt import QApplication, QHBoxLayout app = QApplication([]) w = QWidget() w.setLayout(QHBoxLayout()) b = ThrobbingButton() b.setIcon(QIcon(I('donate.png'))) w.layout().addWidget(b) w.show() b.set_normal_icon_size(64, 64) b.start_animation() app.exec_()
self.animation.setStartValue(smaller) self.animation.setEndValue(size) QMetaObject.invokeMethod(self.animation, 'start', Qt.QueuedConnection) def stop_animation(self): self.animation.stop() self.animation_finished() def paintEvent(self, ev): size = self._icon_size if self._icon_size > 10 else self.iconSize().width() p = QPainter(self) opt = QStyleOptionToolButton() self.initStyleOption(opt) s = self.style() opt.iconSize = QSize(size, size) s.drawComplexControl(s.CC_ToolButton, opt, p, self) if __name__ == '__main__': from PyQt5.Qt import QApplication, QHBoxLayout app = QApplication([]) w = QWidget() w.setLayout(QHBoxLayout()) b = ThrobbingButton() b.setIcon(QIcon(I('donate.png'))) w.layout().addWidget(b) w.show() b.set_normal_icon_size(64, 64) b.start_animation() app.exec_()
def show(self): QWidget.show(self) self.refresh()
class LLT_EditWord(QMainWindow): def __init__(self): super(LLT_EditWord, self).__init__() self.w = QWidget() self.setCentralWidget(self.w) self.verbDic = [] self.nounDic = [] self.adjDic = [] self.phraseDic = [] self.wordList = [] self.verbList = [] self.nounList = [] self.adjList = [] self.phraseList = [] self.index = int(0) self.w.setWindowTitle("Edit Word") self.w.setGeometry(0,0,500, 500) self.vRad = QRadioButton("Verb") self.vRad.setChecked(True) self.nRad = QRadioButton("Noun") self.aRad = QRadioButton("Adjective") self.pRad = QRadioButton("Phrase") self.okBut = QPushButton("OK") self.okBut.clicked.connect(self.OK) self.lookLab = QLabel("Lookup: ") self.lookEntry = QLineEdit() self.lookEntry.setEnabled(False) self.lookBut = QPushButton("Search") self.lookBut.clicked.connect(self.search) self.lookBut.setEnabled(False) self.entryLab = QLabel("Word: ") self.wordEntry = QLineEdit() self.wordEntry.setEnabled(False) self.tranLab = QLabel("Translation: ") self.tranEntry = QLineEdit() self.tranEntry.setEnabled(False) self.saveBut = QPushButton("Save") self.saveBut.clicked.connect(self.save) self.clearBut = QPushButton("Reset") self.clearBut.clicked.connect(self.reset) self.newBut = QPushButton("New word") self.newBut.clicked.connect(self.new) self.exitBut = QPushButton("Exit") self.exitBut.clicked.connect(self.exit) grid = QGridLayout() grid.addWidget(self.vRad, 0, 0) grid.addWidget(self.nRad, 0, 1) grid.addWidget(self.aRad, 0, 2) grid.addWidget(self.pRad, 0, 3) grid.addWidget(self.okBut, 0, 4) grid.addWidget(self.lookLab, 1, 0) grid.addWidget(self.lookEntry, 1, 1, 1, 2) grid.addWidget(self.lookBut, 1, 4) grid.addWidget(self.entryLab, 2, 0) grid.addWidget(self.wordEntry, 2, 1, 1, 3) grid.addWidget(self.tranLab, 3, 0) grid.addWidget(self.tranEntry, 3, 1, 1, 3) grid.addWidget(self.saveBut, 4, 0) grid.addWidget(self.clearBut, 4, 1) grid.addWidget(self.newBut, 4, 2) grid.addWidget(self.exitBut, 4, 3) self.getDics() self.setLists() self.w.setLayout(grid) self.w.show() def search(self): searchWord = self.lookEntry.text().upper() currentDic = [] currentList = [] if self.vRad.isChecked(): currentDic = self.verbDic currentList = self.verbList elif self.nRad.isChecked(): currentDic = self.nounDic currentList = self.nounList elif self.aRad.isChecked(): currentDic = self.adjDic currentList = self.adjList elif self.pRad.isChecked(): currentDic = self.phraseDic currentList = self.phraseList else: msgBox = QMessageBox() msgBox.setText("You must select a dictionary before continuing.") msgBox.exec_() self.vRad.setEnabled(True) self.nRad.setEnabled(True) self.aRad.setEnabled(True) self.pRad.setEnabled(True) self.lookEntry.setEnabled(False) self.lookBut.setEnabled(False) self.wordEntry.setEnabled(False) self.tranEntry.setEnabled(False) if searchWord in self.wordList: self.lookEntry.setEnabled(False) self.lookBut.setEnabled(False) self.wordEntry.setEnabled(True) self.tranEntry.setEnabled(True) self.index = currentList.index(searchWord) oldWord = currentDic[self.index] self.wordEntry.setPlaceholderText(oldWord[0]) self.tranEntry.setPlaceholderText(oldWord[1]) else: msgBox = QMessageBox() msgBox.setText("Word not currently saved in dictionary.") msgBox.exec_(); def OK(self): self.vRad.setEnabled(False) self.nRad.setEnabled(False) self.aRad.setEnabled(False) self.pRad.setEnabled(False) self.lookEntry.setEnabled(True) self.lookBut.setEnabled(True) def save(self): spanWord = self.wordEntry.text().upper() tranWord = self.tranEntry.text().upper() if spanWord in self.wordList: msgBox = QMessageBox() msgBox.setText(spanWord + ' already in dictionary') msgBox.exec_(); else: self.wordList.append(spanWord) newWord = [spanWord,tranWord] self.wordEntry.setEnabled(False) self.tranEntry.setEnabled(False) if self.vRad.isChecked(): self.verbDic[self.index]= newWord v = open('verb.txt','w') json.dump(self.verbDic, v) v.close() elif self.nRad.isChecked(): self.nounDic[self.index]= newWord n = open('noun.txt','w') json.dump(self.nounDic, n) n.close() elif self.aRad.isChecked(): self.adjDic[self.index]= newWord a = open('adj.txt','w') json.dump(self.adjDic, a) a.close() elif self.pRad.isChecked(): self.phraseDic[self.index]= newWord p = open('phrase.txt','w') json.dump(self.phraseDic, p) p.close() else: msgBox = QMessageBox() msgBox.setText("You must select a dictionary before saving word.") msgBox.exec_(); def reset(self): self.vRad.setEnabled(True) self.nRad.setEnabled(True) self.aRad.setEnabled(True) self.pRad.setEnabled(True) self.lookEntry.setEnabled(False) self.lookBut.setEnabled(False) self.wordEntry.setEnabled(False) self.tranEntry.setEnabled(False) self.wordEntry.clear() self.tranEntry.clear() self.lookEntry.setPlaceholderText('') self.wordEntry.setPlaceholderText('') self.tranEntry.setPlaceholderText('') def new(self): self.vRad.setEnabled(True) self.nRad.setEnabled(True) self.aRad.setEnabled(True) self.pRad.setEnabled(True) self.wordEntry.setEnabled(False) self.tranEntry.setEnabled(False) self.wordEntry.clear() self.tranEntry.clear() self.lookEntry.setPlaceholderText('') self.wordEntry.setPlaceholderText('') self.tranEntry.setPlaceholderText('') def exit(self): confirm = QMessageBox.question(self.w, 'Quit', 'Are you sure you want to exit?', QMessageBox.Yes | QMessageBox.No) if confirm == QMessageBox.Yes: self.close() else: pass def getDics(self): try: v=open('verb.txt','r') self.verbDic=json.load(v) v.close() except: self.verbDic = [] try: n=open('noun.txt','r') self.nounDic=json.load(n) n.close() except: self.nounDic = [] try: p=open('phrase.txt','r') self.phraseDic=json.load(p) p.close() except: self.phraseDic = [] try: a=open('adj.txt','r') self.adjDic=json.load(a) a.close() except: self.phraseDic = [] def setLists(self): for item in self.verbDic: self.wordList.append(item[0]) self.verbList.append(item[0]) for item in self.nounDic: self.wordList.append(item[0]) self.nounList.append(item[0]) for item in self.adjDic: self.wordList.append(item[0]) self.adjList.append(item[0]) for item in self.phraseDic: self.wordList.append(item[0]) self.phraseList.append(item[0])
class SynthesisQWidget(QWidget): """ Class who manage Synthesis view with Host and Services QWidgets """ def __init__(self, parent=None): super(SynthesisQWidget, self).__init__(parent) # Fields self.host_widget = HostQWidget() self.services_widget = ServicesQWidget() self.line_search = QLineEdit() self.completer = QCompleter() self.hint_widget = QWidget() def initialize_synthesis(self): """ Initialize Synthesis QWidget """ synthesis_layout = QVBoxLayout() synthesis_layout.setContentsMargins(0, 0, 0, 0) self.setLayout(synthesis_layout) # Search widget search_widget = self.get_search_widget() synthesis_layout.addWidget(search_widget) # Host widget self.host_widget.initialize() self.host_widget.setMaximumHeight(self.width() * 0.5) synthesis_layout.addWidget(self.host_widget) # Hint Widget hint_text = _( '<h4>Dahsboard</h4>' '<ul><li>At the top of App, ' 'you have a dashboard that summarizes the number of items per state.</li></ul>' '<h4>Tabs</h4>' '<ul><li><h4>Host Synthesis</h4></li>' 'Tap in the search bar to view a host and its services.' '<li><h4>Problems</h4></li>' 'The "Problems" tab will show you all problems detected in your backend.' '<li><h4>Spy Hosts</h4></li>' 'A "Spy Host" will keep you regularly informed of his condition. ' 'You will also see problems detected for this host, in the "Spy Hosts" panel.</ul>' '<h4>Alignak</h4>' '<ul><li>You can see your backend status and daemons if available, ' 'as well as your profile.</li></ul>' '<h4>Livestate</h4>' '<ul><li>In the livestate, you can see global state of your monitored items.</li></ul>' '<h4>Events</h4>' '<ul><li>Events will show you informative messages your actions inside App.</li></ul>' ) hint_layout = QVBoxLayout(self.hint_widget) hint_label = QLabel(hint_text) hint_label.setObjectName('subtitle') hint_layout.addWidget(hint_label) synthesis_layout.addWidget(self.hint_widget) # Services widget self.services_widget.initialize() synthesis_layout.addWidget(self.services_widget) # Align all widgets to Top synthesis_layout.setAlignment(Qt.AlignTop) def get_search_widget(self): """ Create and return the search QWidget :return: search QWidget :rtype: QWidget """ widget = QWidget() layout = QHBoxLayout() layout.setSpacing(0) widget.setLayout(layout) # Search label search_lbl = QLabel(_('Search Host')) search_lbl.setObjectName('bordertitle') search_lbl.setFixedHeight(25) search_lbl.setToolTip(_('Search Host')) layout.addWidget(search_lbl) # QLineEdit self.line_search.setFixedHeight(search_lbl.height()) layout.addWidget(self.line_search) self.create_line_search([]) return widget def create_line_search(self, hostnames_list): """ Add all hosts to QLineEdit and set QCompleter :param hostnames_list: list of host names :type hostnames_list: list """ # Get QStringListModel model = self.completer.model() if not model: model = QStringListModel() model.setStringList(hostnames_list) # Configure QCompleter from model self.completer.setFilterMode(Qt.MatchContains) self.completer.setCaseSensitivity(Qt.CaseInsensitive) self.completer.setModel(model) self.completer.popup().setObjectName('popup') # Add completer to QLineEdit self.line_search.setCompleter(self.completer) self.line_search.setPlaceholderText( _('Type a host name to display its data')) self.line_search.setToolTip(_('Type a host name to display its data')) def update_synthesis(self, host, services, not_spied): """ Update Synthesis QWidget with given host and services :param host: host item :type host: alignak_app.items.host.Host :param services: list of services attached to host :type services: list :param not_spied: define if host is spied or not :type not_spied: bool """ self.host_widget.spy_btn.setEnabled(not_spied) if host: logger.info('Display %s in synthesis view', host.name) # Update Qwidgets self.host_widget.update_host(host) self.services_widget.update_widget(services) self.hint_widget.hide() self.host_widget.show() self.services_widget.show() # If the service element does not have the same ID as the host, reset to None if self.services_widget.service_data_widget.service_item: if self.services_widget.service_data_widget.service_item.data['host'] != \ self.host_widget.host_item.item_id: self.services_widget.service_data_widget.service_item = None else: self.host_widget.hide() self.services_widget.hide() self.hint_widget.show()
import sys from PyQt5.Qt import QApplication,QWidget from UI.MainForm import MainForm if __name__ == '__main__': app = QApplication(sys.argv) ui = MainForm() widget = QWidget() ui.setupUi(widget) ui.init(widget) widget.show() sys.exit(app.exec_())
def show(self): self.move(0, 1) self.parent_resized() QWidget.show(self) self.raise_()
class LLT_Lookup(QMainWindow): def __init__(self): super(LLT_Lookup, self).__init__() self.w = QWidget() self.setCentralWidget(self.w) self.verbDic = [] self.nounDic = [] self.adjDic = [] self.phraseDic = [] self.wordList = [] self.wordDic = [] self.index = int(0) self.w.setWindowTitle("Word Lookup") self.w.setGeometry(0,0,500, 500) self.lookLab = QLabel("Lookup: ") self.lookEntry = QLineEdit() self.lookBut = QPushButton("Search") self.lookBut.clicked.connect(self.search) self.wordLab = QLabel("Word: ") self.resultWord = QLabel() self.tranLab = QLabel("Translation: ") self.resultTran = QLabel() self.newBut = QPushButton("New Search") self.newBut.clicked.connect(self.new) self.exitBut = QPushButton("Exit") self.exitBut.clicked.connect(self.exit) grid = QGridLayout() grid.addWidget(self.lookLab, 0, 0) grid.addWidget(self.lookEntry, 0, 1, 1, 2) grid.addWidget(self.lookBut, 0, 3) grid.addWidget(self.wordLab, 1, 0) grid.addWidget(self.resultWord, 1, 1, 1, 3) grid.addWidget(self.tranLab, 2, 0) grid.addWidget(self.resultTran, 2, 1, 1, 3) grid.addWidget(self.newBut, 3, 0) grid.addWidget(self.exitBut, 3, 3) self.getDics() self.setLists() self.w.setLayout(grid) self.w.show() def search(self): searchWord = self.lookEntry.text().upper().strip() if searchWord in self.wordList: self.lookEntry.setEnabled(False) self.lookBut.setEnabled(False) self.index = self.wordList.index(searchWord) foundWord = self.wordDic[self.index] self.resultWord.setText(foundWord[0]) self.resultTran.setText(foundWord[1]) else: msgBox = QMessageBox() msgBox.setText("Word not currently saved in dictionary.") msgBox.exec_(); def new(self): self.lookEntry.setEnabled(True) self.lookBut.setEnabled(True) self.resultWord.setText('') self.resultTran.setText('') self.lookEntry.clear() def exit(self): confirm = QMessageBox.question(self.w, 'Quit', 'Are you sure you want to exit?', QMessageBox.Yes | QMessageBox.No) if confirm == QMessageBox.Yes: self.close() else: pass def getDics(self): try: v=open('verb.txt','r') self.verbDic=json.load(v) v.close() except: self.verbDic = [] try: n=open('noun.txt','r') self.nounDic=json.load(n) n.close() except: self.nounDic = [] try: p=open('phrase.txt','r') self.phraseDic=json.load(p) p.close() except: self.phraseDic = [] try: a=open('adj.txt','r') self.adjDic=json.load(a) a.close() except: self.phraseDic = [] def setLists(self): for item in self.verbDic: self.wordList.append(item[0]) self.wordDic.append(item) for item in self.nounDic: self.wordList.append(item[0]) self.wordDic.append(item) for item in self.adjDic: self.wordList.append(item[0]) self.wordDic.append(item) for item in self.phraseDic: self.wordList.append(item[0]) self.wordDic.append(item)
from PyQt5.Qt import QApplication, QWidget, QLabel import sys if __name__ == '__main__': # 创建QApplication类的实例 app = QApplication(sys.argv) # 创建一个窗口 window = QWidget() # 设置窗口标题 window.setWindowTitle('Hello World!') # 设置窗口尺寸 window.resize(400, 400) # 移动窗口位置 window.move(1000, 300) # 创建label控件 label = QLabel(window) # 为控件设置文本 label.setText('Hello World!') # 移动空间的位置 label.move(160, 160) # 显示窗口 window.show() # 进入程序的主循环,并通过exit函数确保主循环安全结束 sys.exit(app.exec_())
class SynthesisQWidget(QWidget): """ Class who manage Synthesis view with Host and Services QWidgets """ def __init__(self, parent=None): super(SynthesisQWidget, self).__init__(parent) # Fields self.host_widget = HostQWidget() self.services_widget = ServicesQWidget() self.line_search = QLineEdit() self.completer = QCompleter() self.hint_widget = QWidget() def initialize_synthesis(self): """ Initialize Synthesis QWidget """ synthesis_layout = QVBoxLayout() synthesis_layout.setContentsMargins(0, 0, 0, 0) self.setLayout(synthesis_layout) # Search widget search_widget = self.get_search_widget() synthesis_layout.addWidget(search_widget) # Host widget self.host_widget.initialize() self.host_widget.setMaximumHeight(self.width() * 0.5) synthesis_layout.addWidget(self.host_widget) # Hint Widget hint_text = _( '<h4>Dahsboard</h4>' '<ul><li>At the top of App, ' 'you have a dashboard that summarizes the number of items per state.</li></ul>' '<h4>Tabs</h4>' '<ul><li><h4>Host Synthesis</h4></li>' 'Tap in the search bar to view a host and its services.' '<li><h4>Problems</h4></li>' 'The "Problems" tab will show you all problems detected in your backend.' '<li><h4>Spy Hosts</h4></li>' 'A "Spy Host" will keep you regularly informed of his condition. ' 'You will also see problems detected for this host, in the "Spy Hosts" panel.</ul>' '<h4>Alignak</h4>' '<ul><li>You can see your backend status and daemons if available, ' 'as well as your profile.</li></ul>' '<h4>Livestate</h4>' '<ul><li>In the livestate, you can see global state of your monitored items.</li></ul>' '<h4>Events</h4>' '<ul><li>Events will show you informative messages your actions inside App.</li></ul>' ) hint_layout = QVBoxLayout(self.hint_widget) hint_label = QLabel(hint_text) hint_label.setObjectName('subtitle') hint_layout.addWidget(hint_label) synthesis_layout.addWidget(self.hint_widget) # Services widget self.services_widget.initialize() synthesis_layout.addWidget(self.services_widget) # Align all widgets to Top synthesis_layout.setAlignment(Qt.AlignTop) def get_search_widget(self): """ Create and return the search QWidget :return: search QWidget :rtype: QWidget """ widget = QWidget() layout = QHBoxLayout() layout.setSpacing(0) widget.setLayout(layout) # Search label search_lbl = QLabel(_('Search Host')) search_lbl.setObjectName('bordertitle') search_lbl.setFixedHeight(25) search_lbl.setToolTip(_('Search Host')) layout.addWidget(search_lbl) # QLineEdit self.line_search.setFixedHeight(search_lbl.height()) layout.addWidget(self.line_search) self.create_line_search([]) return widget def create_line_search(self, hostnames_list): """ Add all hosts to QLineEdit and set QCompleter :param hostnames_list: list of host names :type hostnames_list: list """ # Get QStringListModel model = self.completer.model() if not model: model = QStringListModel() model.setStringList(hostnames_list) # Configure QCompleter from model self.completer.setFilterMode(Qt.MatchContains) self.completer.setCaseSensitivity(Qt.CaseInsensitive) self.completer.setModel(model) self.completer.popup().setObjectName('popup') # Add completer to QLineEdit self.line_search.setCompleter(self.completer) self.line_search.setPlaceholderText(_('Type a host name to display its data')) self.line_search.setToolTip(_('Type a host name to display its data')) def update_synthesis(self, host, services, not_spied): """ Update Synthesis QWidget with given host and services :param host: host item :type host: alignak_app.items.host.Host :param services: list of services attached to host :type services: list :param not_spied: define if host is spied or not :type not_spied: bool """ self.host_widget.spy_btn.setEnabled(not_spied) if host: logger.info('Display %s in synthesis view', host.name) # Update Qwidgets self.host_widget.update_host(host) self.services_widget.update_widget(services) self.hint_widget.hide() self.host_widget.show() self.services_widget.show() # If the service element does not have the same ID as the host, reset to None if self.services_widget.service_data_widget.service_item: if self.services_widget.service_data_widget.service_item.data['host'] != \ self.host_widget.host_item.item_id: self.services_widget.service_data_widget.service_item = None else: self.host_widget.hide() self.services_widget.hide() self.hint_widget.show()
import sys from PyQt5.Qt import QApplication,QWidget if __name__=='__main__': app = QApplication(sys.argv) dialog = QWidget() dialog.show() sys.exit(app.exec_())
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'ipetrash' from PyQt5.Qt import QApplication, QLabel, QVBoxLayout, QWidget app = QApplication([]) app.setStyleSheet('QLabel { font: bold italic }') label_1 = QLabel() label_1.setText('Hello World!!!') label_2 = QLabel() label_2.setText('nothing...') layout = QVBoxLayout() layout.addWidget(label_1) layout.addWidget(label_2) mw = QWidget() mw.setLayout(layout) mw.show() app.exec()
class LLT_AddWord(QMainWindow): def __init__(self): super(LLT_AddWord, self).__init__() self.w = QWidget() self.setCentralWidget(self.w) self.verbDic = [] self.nounDic = [] self.adjDic = [] self.phraseDic = [] self.wordList = [] self.w.setWindowTitle("Add Word") self.w.setGeometry(0, 0, 500, 500) self.vRad = QRadioButton("Verb") self.vRad.setChecked(True) self.nRad = QRadioButton("Noun") self.aRad = QRadioButton("Adjective") self.pRad = QRadioButton("Phrase") self.okBut = QPushButton("OK") self.okBut.clicked.connect(self.OK) self.entryLab = QLabel("Word: ") self.wordEntry = QLineEdit() self.wordEntry.setEnabled(False) self.tranLab = QLabel("Translation: ") self.tranEntry = QLineEdit() self.tranEntry.setEnabled(False) self.checkBut = QPushButton("Check") self.checkBut.clicked.connect(self.check) self.saveBut = QPushButton("Save") self.saveBut.clicked.connect(self.save) self.clearBut = QPushButton("Clear") self.clearBut.clicked.connect(self.clear) self.newBut = QPushButton("New word") self.newBut.clicked.connect(self.new) self.exitBut = QPushButton("Exit") self.exitBut.clicked.connect(self.exit) grid = QGridLayout() grid.addWidget(self.vRad, 0, 0) grid.addWidget(self.nRad, 0, 1) grid.addWidget(self.aRad, 0, 2) grid.addWidget(self.pRad, 0, 3) grid.addWidget(self.okBut, 0, 4) grid.addWidget(self.entryLab, 1, 0) grid.addWidget(self.wordEntry, 1, 1, 1, 3) grid.addWidget(self.tranLab, 2, 0) grid.addWidget(self.tranEntry, 2, 1, 1, 3) grid.addWidget(self.checkBut, 3, 0) grid.addWidget(self.saveBut, 3, 1) grid.addWidget(self.clearBut, 3, 2) grid.addWidget(self.newBut, 3, 3) grid.addWidget(self.exitBut, 3, 4) self.getDics() self.setLists() self.w.setLayout(grid) self.w.show() def OK(self): self.vRad.setEnabled(False) self.nRad.setEnabled(False) self.aRad.setEnabled(False) self.pRad.setEnabled(False) self.okBut.setEnabled(False) self.wordEntry.setEnabled(True) self.tranEntry.setEnabled(True) def check(self): word = self.wordEntry.text().upper() if word in self.wordList: msgBox = QMessageBox() msgBox.setText(word + ' already in dictionary') msgBox.exec_() else: msgBox = QMessageBox() msgBox.setText(word + ' not in dictionary yet') msgBox.exec_() def save(self): spanWord = self.wordEntry.text().upper() tranWord = self.tranEntry.text().upper() if spanWord in self.wordList: msgBox = QMessageBox() msgBox.setText(spanWord + ' already in dictionary') msgBox.exec_() else: self.wordList.append(spanWord) newWord = [spanWord, tranWord] self.wordEntry.setEnabled(False) self.tranEntry.setEnabled(False) if self.vRad.isChecked(): self.verbDic.append(newWord) v = open('verb.txt', 'w') json.dump(self.verbDic, v) v.close() elif self.nRad.isChecked(): self.nounDic.append(newWord) n = open('noun.txt', 'w') json.dump(self.nounDic, n) n.close() elif self.aRad.isChecked(): self.adjDic.append(newWord) a = open('adj.txt', 'w') json.dump(self.adjDic, a) a.close() elif self.pRad.isChecked(): self.phraseDic.append(newWord) p = open('phrase.txt', 'w') json.dump(self.phraseDic, p) p.close() else: msgBox = QMessageBox() msgBox.setText( "You must select a dictionary before saving word.") msgBox.exec_() def clear(self): self.vRad.setEnabled(True) self.nRad.setEnabled(True) self.aRad.setEnabled(True) self.pRad.setEnabled(True) self.okBut.setEnabled(True) self.wordEntry.setEnabled(False) self.tranEntry.setEnabled(False) self.wordEntry.clear() self.tranEntry.clear() def new(self): self.vRad.setEnabled(True) self.nRad.setEnabled(True) self.aRad.setEnabled(True) self.pRad.setEnabled(True) self.okBut.setEnabled(True) self.wordEntry.setEnabled(False) self.tranEntry.setEnabled(False) self.wordEntry.clear() self.tranEntry.clear() def exit(self): confirm = QMessageBox.question(self.w, 'Quit', 'Are you sure you want to exit?', QMessageBox.Yes | QMessageBox.No) if confirm == QMessageBox.Yes: self.close() else: pass def getDics(self): try: v = open('verb.txt', 'r') self.verbDic = json.load(v) v.close() except: self.verbDic = [] try: n = open('noun.txt', 'r') self.nounDic = json.load(n) n.close() except: self.nounDic = [] try: p = open('phrase.txt', 'r') self.phraseDic = json.load(p) p.close() except: self.phraseDic = [] try: a = open('adj.txt', 'r') self.adjDic = json.load(a) a.close() except: self.phraseDic = [] def setLists(self): for item in self.verbDic: self.wordList.append(item[0]) for item in self.nounDic: self.wordList.append(item[0]) for item in self.phraseDic: self.wordList.append(item[0]) for item in self.adjDic: self.wordList.append(item[0])