def version3(): """Adds window title and multiple widgets.""" def create_window(): window = QtGui.QMainWindow() window.setWindowTitle('Hierarchy Converter') container = QtGui.QWidget(window) label = QtGui.QLabel('Prefix:', container) textbox = QtGui.QLineEdit(container) button = QtGui.QPushButton('Convert', container) layout = QtGui.QHBoxLayout(container) container.setLayout(layout) # Add them to the layout, first widget added is left-most layout.addWidget(label) layout.addWidget(textbox) layout.addWidget(button) window.setCentralWidget(container) return window if __name__ == '__main__': app = QtGui.QApplication([]) win = create_window() win.show() app.exec_()
def version1(): """Very basic first version.""" def create_window(): win = QtGui.QMainWindow() return win if __name__ == '__main__': app = QtGui.QApplication([]) win = create_window() win.show() app.exec_()
def _pytest(): import random controller = HierarchyConverterController() def nextsel(): return random.choice([[], ['single'], ['single', 'double']]) def onconvert(prefix): print 'Convert clicked! Prefix:', prefix controller.selectionChanged.emit(nextsel()) app = QtGui.QApplication([]) win = create_window(controller) win.convertClicked.connect(onconvert) win.show() app.exec_()
def version2(): """Shows creating textbox, layout, and central widget.""" def create_window(): window = QtGui.QMainWindow() #(1) container = QtGui.QWidget(window) textbox = QtGui.QLineEdit(container) layout = QtGui.QHBoxLayout(container) #(2) container.setLayout(layout) layout.addWidget(textbox) #(3) window.setCentralWidget(container) #(4) return window if __name__ == '__main__': app = QtGui.QApplication([]) win = create_window() win.show() app.exec_()
def version4(): """Adds convertClicked signal and support.""" class ConverterWindow(QtGui.QMainWindow): #(1) convertClicked = Signal(str) def create_window(): window = ConverterWindow() window.setWindowTitle('Hierarchy Converter') container = QtGui.QWidget(window) label = QtGui.QLabel('Prefix:', container) textbox = QtGui.QLineEdit(container) button = QtGui.QPushButton('Convert', container) def onclick(): #(2) window.convertClicked.emit(textbox.text()) button.clicked.connect(onclick) #(3) layout = QtGui.QHBoxLayout(container) container.setLayout(layout) layout.addWidget(label) layout.addWidget(textbox) layout.addWidget(button) window.setCentralWidget(container) return window if __name__ == '__main__': def onconvert(prefix): #(4) print 'Convert clicked! Prefix:', prefix app = QtGui.QApplication([]) win = create_window() win.convertClicked.connect(onconvert) #(5) win.show() app.exec_()