Pyqt4 MainWindow->Form->Form impossible? - python

i want to display form after form that is display after mainwindow.
This is my code. I success display form after mainwindow.
but MainWindow->Form->Form not work....
please help my brain
my brain is very hot...
main.py
class main(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
app=QtGui.QApplication(sys.argv)
myapp=main()
myapp.show()
app.exec_()
mainUI.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main.ui'
#
# Created: Tue Mar 17 17:24:48 2015
# by: PyQt4 UI code generator 4.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
import thread
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
def start(msg):
while 1:
None
def start2(msg):
while 1:
None
global Form
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(722, 265)
MainWindow.setMinimumSize(QtCore.QSize(722, 265))
MainWindow.setMaximumSize(QtCore.QSize(722, 265))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("image/images.jpg")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setEnabled(True)
self.label.setGeometry(QtCore.QRect(-30, -50, 751, 341))
self.label.setMouseTracking(False)
self.label.setAcceptDrops(False)
self.label.setText(_fromUtf8(""))
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("image/main.png")))
self.label.setObjectName(_fromUtf8("label"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 722, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menu = QtGui.QMenu(self.menubar)
self.menu.setObjectName(_fromUtf8("menu"))
MainWindow.setMenuBar(self.menubar)
self.action = QtGui.QAction(MainWindow)
self.action.setObjectName(_fromUtf8("action"))
self.menu.addAction(self.action)
self.menubar.addAction(self.menu.menuAction())
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.action, QtCore.SIGNAL(_fromUtf8("triggered()")), self.create_child)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def create_child(self):
#here put the code that creates the new window and shows it.
global Form
Form=QtGui.QWidget()
child = release_Form()
child.setupReleaseUi(Form)
thread.start_new_thread(start,(Form,))
Form.show()
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Smart Update", None))
self.menu.setTitle(_translate("MainWindow", "릴리즈노트", None))
self.action.setText(_translate("MainWindow", "릴리즈노트", None))
class release_Form(object):
def setupReleaseUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(499, 406)
self.New = QtGui.QPlainTextEdit(Form)
self.New.setGeometry(QtCore.QRect(20, 20, 104, 301))
self.New.setObjectName(_fromUtf8("New"))
self.Modify = QtGui.QPlainTextEdit(Form)
self.Modify.setGeometry(QtCore.QRect(150, 20, 104, 301))
self.Modify.setObjectName(_fromUtf8("Modify"))
self.Delete = QtGui.QPlainTextEdit(Form)
self.Delete.setGeometry(QtCore.QRect(280, 20, 104, 301))
self.Delete.setObjectName(_fromUtf8("Delete"))
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(410, 70, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.New, QtCore.SIGNAL(_fromUtf8("textChanged()")), self.Modify.clear)
QtCore.QObject.connect(self.Modify, QtCore.SIGNAL(_fromUtf8("textChanged()")), self.New.centerCursor)
QtCore.QObject.connect(self.Delete, QtCore.SIGNAL(_fromUtf8("textChanged()")), self.Delete.centerCursor)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.create_alert)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
def create_alert(self):
global Form
Form1=QtGui.QWidget()
alert_Form.show()
child1 = alert_Form()
child1.setupAlertUi(Form1)
thread.start_new_thread(start2,(Form,))
Form1.show()
class alert_Form(object):
def setupAlertUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(431, 109)
Form.setMinimumSize(QtCore.QSize(431, 109))
Form.setMaximumSize(QtCore.QSize(431, 109))
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(-10, -10, 451, 121))
self.label.setText(_fromUtf8(""))
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("image/alert.png")))
self.label.setScaledContents(True)
self.label.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Alert", None))

you should connect function.
and you call other Form like this
if you want only once call Form, remove thread.
def create_setting(self):
global Dialog
Dialog=QtGui.QDialog()
thread.start_new_thread(start,(Dialog,))
child = settingUI.Ui_Form()
child.setupUi(Dialog)
Dialog.setWindowFlags(Dialog.windowFlags()|QtCore.Qt.WindowSystemMenuHint|QtCore.Qt.WindowMinMaxButtonsHint)
Dialog.show()

Related

hide a frame after 3 seconds/ by calling a function (need to done in both way)

I create 2 frames in the same window with some attributes in it. now am able to call it separately or can display both
from PyQt4 import QtCore, QtGui
import time
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def frame1(self,Dialog):
self.frame = QtGui.QFrame(Dialog)
self.frame.setGeometry(QtCore.QRect(40, 20, 311, 391))
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName(_fromUtf8("frame"))
self.label = QtGui.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(120, 180, 72, 23))
self.label.setObjectName(_fromUtf8("label"))
self.label.setText(_translate("Dialog", "omniOS", None))
def frame2(self,Dialog):
self.frame_2 = QtGui.QFrame(Dialog)
self.frame_2.setGeometry(QtCore.QRect(30, 30, 311, 391))
self.frame_2.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtGui.QFrame.Raised)
self.frame_2.setObjectName(_fromUtf8("frame_2"))
self.label_2 = QtGui.QLabel(self.frame_2)
self.label_2.setGeometry(QtCore.QRect(50, 230, 72, 31))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.lineEdit = QtGui.QLineEdit(self.frame_2)
self.lineEdit.setGeometry(QtCore.QRect(150, 230, 113, 33))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.label_2.setText(_translate("Dialog", "Login", None))
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(391, 437)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "OmniOS", None))
self.frame1(Dialog)
self.frame2(Dialog)
if __name__=="__main__":
ui=Ui_Dialog()
import sys
a = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
ui.setupUi(w)
w.show()
sys.exit(a.exec_())
what I need to do is, I need to get the first window after a 3 second I need to get the second window + hide the first one without overlapping it
self.frame1(Dialog) # call this when the program start
self.frame2(Dialog) # call this after 3 second self.frame1(Dialog)
Any help would be appreciated ThankYou
That a code works does not imply that it is correct, for example I see that you have combined 2 codes generated by Qt Designer and obviously they are going to overlap, the correct thing is to have 2 windows that are exchanged as shown below using QTimer.
from PyQt4 import QtCore, QtGui
import time
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog1(object):
def setupUi(self, Dialog):
self.frame = QtGui.QFrame(Dialog)
self.frame.setGeometry(QtCore.QRect(40, 20, 311, 391))
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName(_fromUtf8("frame"))
self.label = QtGui.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(120, 180, 72, 23))
self.label.setObjectName(_fromUtf8("label"))
self.label.setText(_translate("Dialog", "omniOS", None))
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(391, 437)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "OmniOS", None))
class Ui_Dialog2(object):
def setupUi(self, Dialog):
self.frame_2 = QtGui.QFrame(Dialog)
self.frame_2.setGeometry(QtCore.QRect(30, 30, 311, 391))
self.frame_2.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtGui.QFrame.Raised)
self.frame_2.setObjectName(_fromUtf8("frame_2"))
self.label_2 = QtGui.QLabel(self.frame_2)
self.label_2.setGeometry(QtCore.QRect(50, 230, 72, 31))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.lineEdit = QtGui.QLineEdit(self.frame_2)
self.lineEdit.setGeometry(QtCore.QRect(150, 230, 113, 33))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.label_2.setText(_translate("Dialog", "Login", None))
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(391, 437)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.retranslateUi(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "OmniOS", None))
class Dialog1(QtGui.QDialog, Ui_Dialog1):
def __init__(self, parent=None):
super(Dialog1, self).__init__(parent)
self.setupUi(self)
class Dialog2(QtGui.QDialog, Ui_Dialog2):
def __init__(self, parent=None):
super(Dialog2, self).__init__(parent)
self.setupUi(self)
if __name__=="__main__":
import sys
a = QtGui.QApplication(sys.argv)
w1 = Dialog1()
w2 = Dialog2()
def on_timeout():
w1.hide()
w2.show()
w1.show()
QtCore.QTimer.singleShot(3000, on_timeout)
sys.exit(a.exec_())

PyQt transferring a list between dialog and window

Very much a beginner on the UI side of things!
I'm trying to transfer a list from a dialog to the main window (when a button is pressed) and then transfer a new list back to the window when the dialog is accepted.
Here's a MWE of what I'm working on to give you an idea.
It should print out the following,
Desired output:
[b]
.
[b]
[a]
.
[a]
Achieved output:
[b]
.
[b]
[a]
.
[b]
Thanks in advance!
MWE:
import sys
from PyQt4 import QtCore, QtGui#pyqt5, QtWidgets
from Scrabble_gui import Ui_MainWindow
from Input_letters_gui import Ui_Dialog as Popup
class ScrabbleGuiProgram(Ui_MainWindow):
def __init__(self, dialog):
Ui_MainWindow.__init__(self)
self.setupUi(dialog)
self.In_letters.clicked.connect(self.Input_letters)
def Input_letters(self):
"""Prints "b" and opens pop up.
after popup is closed, prints "a"
"""
letter_list = ["b"]
#app = QtGui.QApplication(sys.argv)
print(letter_list)
print(".")
PUDialog = QtGui.QDialog()
pop = Input_letters_popup(PUDialog, letter_list)
PUDialog.exec_()
print(".")
print(letter_list)
class Input_letters_popup(Popup):
def __init__(self, dialog, letter_list):
"""when open prints incoming list ("b")
"""
print(letter_list)
Popup.__init__(self)
self.setupUi(dialog)
self.buttonBox.accepted.connect(self.Get_letters)#lambda: self.Get_letters)
#self.buttonBox.rejected.connect()
def Get_letters(self):
"""on popup exit prints new list ("a")
"""
letters = ["a"]
print(letters)
return letters
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QMainWindow()
prog = ScrabbleGuiProgram(Dialog)#Input_letters_popup(Dialog)
Dialog.show()
sys.exit(app.exec_())
Scrabble_gui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Scrabble.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(406, 267)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.Gen_words = QtGui.QPushButton(self.centralwidget)
self.Gen_words.setGeometry(QtCore.QRect(250, 20, 121, 28))
self.Gen_words.setObjectName(_fromUtf8("Gen_words"))
self.Long_word = QtGui.QLineEdit(self.centralwidget)
self.Long_word.setGeometry(QtCore.QRect(229, 100, 141, 22))
self.Long_word.setReadOnly(True)
self.Long_word.setObjectName(_fromUtf8("Long_word"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(279, 180, 41, 21))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_3 = QtGui.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(99, 143, 131, 21))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.High_score = QtGui.QLineEdit(self.centralwidget)
self.High_score.setGeometry(QtCore.QRect(319, 180, 51, 22))
self.High_score.setReadOnly(True)
self.High_score.setObjectName(_fromUtf8("High_score"))
self.High_word = QtGui.QLineEdit(self.centralwidget)
self.High_word.setGeometry(QtCore.QRect(229, 143, 141, 22))
self.High_word.setReadOnly(True)
self.High_word.setObjectName(_fromUtf8("High_word"))
self.In_letters = QtGui.QPushButton(self.centralwidget)
self.In_letters.setGeometry(QtCore.QRect(90, 57, 121, 28))
self.In_letters.setObjectName(_fromUtf8("In_letters"))
self.Gen_letters = QtGui.QPushButton(self.centralwidget)
self.Gen_letters.setGeometry(QtCore.QRect(90, 20, 121, 28))
self.Gen_letters.setObjectName(_fromUtf8("Gen_letters"))
self.letters = QtGui.QListWidget(self.centralwidget)
self.letters.setGeometry(QtCore.QRect(20, 50, 41, 161))
self.letters.setObjectName(_fromUtf8("letters"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(145, 100, 81, 20))
self.label.setObjectName(_fromUtf8("label"))
self.label_4 = QtGui.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(20, 20, 41, 21))
self.label_4.setObjectName(_fromUtf8("label_4"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 406, 26))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Descrabbler", None))
self.Gen_words.setText(_translate("MainWindow", "Best Words", None))
self.label_2.setText(_translate("MainWindow", "Score", None))
self.label_3.setText(_translate("MainWindow", "Highest Scoring Word", None))
self.In_letters.setText(_translate("MainWindow", "Input Letters", None))
self.Gen_letters.setText(_translate("MainWindow", "Generate Letters", None))
self.label.setText(_translate("MainWindow", "Longest Word", None))
self.label_4.setText(_translate("MainWindow", "Letters", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Input_letters_gui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Input_letters.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(400, 173)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 90, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.ltr1 = QtGui.QLineEdit(Dialog)
self.ltr1.setGeometry(QtCore.QRect(20, 40, 31, 22))
self.ltr1.setObjectName(_fromUtf8("ltr1"))
self.ltr2 = QtGui.QLineEdit(Dialog)
self.ltr2.setGeometry(QtCore.QRect(60, 40, 31, 22))
self.ltr2.setObjectName(_fromUtf8("ltr2"))
self.ltr3 = QtGui.QLineEdit(Dialog)
self.ltr3.setGeometry(QtCore.QRect(100, 40, 31, 22))
self.ltr3.setObjectName(_fromUtf8("ltr3"))
self.ltr4 = QtGui.QLineEdit(Dialog)
self.ltr4.setGeometry(QtCore.QRect(140, 40, 31, 22))
self.ltr4.setObjectName(_fromUtf8("ltr4"))
self.ltr5 = QtGui.QLineEdit(Dialog)
self.ltr5.setGeometry(QtCore.QRect(180, 40, 31, 22))
self.ltr5.setObjectName(_fromUtf8("ltr5"))
self.ltr6 = QtGui.QLineEdit(Dialog)
self.ltr6.setGeometry(QtCore.QRect(220, 40, 31, 22))
self.ltr6.setObjectName(_fromUtf8("ltr6"))
self.ltr7 = QtGui.QLineEdit(Dialog)
self.ltr7.setGeometry(QtCore.QRect(260, 40, 31, 22))
self.ltr7.setObjectName(_fromUtf8("ltr7"))
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Input Letters", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())

How to refresh items in Qcombobox [python]?

I have a list of data which is shown in the Qcombobox (using 'addItems')
Now - if the list is changed by loading data from a file with pushbutton - i don't see the new data in the combobx.
The new data is there (I can print it after loading)
What do I miss?
below is the code for the simplified gui
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QFileDialog
import pandas as pd
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def __init__(self):
self.comboData=['None']
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(456, 172)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.comboBox = QtGui.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(120, 60, 69, 22))
self.comboBox.setObjectName(_fromUtf8("comboBox"))
self.comboBox.setEditable(True)
self.comboBox.addItems(self.comboData)
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(260, 60, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(self.load)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 456, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "Load", None))
def load(self):
fileName=QFileDialog.getOpenFileName(MainWindow,'Load File')
data_pd=pd.read_csv(fileName,index_col=0,na_filter=False)
self.comboData=list(data_pd.var1)
print(self.comboData)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
you only load the data to self.comboData but do not load them to self.comboBox.
At the end of
def load(self):
add the following lines:
self.comboBox.clear() # delete all items from comboBox
self.comboBox.addItems(self.comboData) # add the actual content of self.comboData
As the model used by QComboBox emits the dataChanged() signal whenever the data in an item are changed, the combobox is repainted automatically and it is not necessary to update the combobox by
self.comboBox.update()

Python - Pyqt: Duplicate output when I use setCurrentIndex

When I press the button for go to a diferent page using setCurrentIndex I have problems with duplicate output values because if I set value "1" on QLineEdit on home_page for example and then go to conf_page and now I back to home_page, when I press the OK button for print content of QLineEdit for example, I get a duplicate output.
My code.
Project.py file.
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(210, 191)
self.frame = QtGui.QFrame(Dialog)
self.frame.setGeometry(QtCore.QRect(0, 0, 211, 61))
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName(_fromUtf8("frame"))
self.pushButton_2 = QtGui.QPushButton(self.frame)
self.pushButton_2.setGeometry(QtCore.QRect(20, 20, 75, 23))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton_3 = QtGui.QPushButton(self.frame)
self.pushButton_3.setGeometry(QtCore.QRect(110, 20, 75, 23))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.stackedWidget = QtGui.QStackedWidget(Dialog)
self.stackedWidget.setGeometry(QtCore.QRect(0, 60, 211, 131))
self.stackedWidget.setObjectName(_fromUtf8("stackedWidget"))
self.page = QtGui.QWidget()
self.page.setObjectName(_fromUtf8("page"))
self.lineEdit = QtGui.QLineEdit(self.page)
self.lineEdit.setGeometry(QtCore.QRect(50, 50, 113, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.pushButton = QtGui.QPushButton(self.page)
self.pushButton.setGeometry(QtCore.QRect(70, 90, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.stackedWidget.addWidget(self.page)
self.page_2 = QtGui.QWidget()
self.page_2.setObjectName(_fromUtf8("page_2"))
self.stackedWidget.addWidget(self.page_2)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.pushButton_2.setText(_translate("Dialog", "PushButton", None))
self.pushButton_3.setText(_translate("Dialog", "PushButton", None))
self.pushButton.setText(_translate("Dialog", "PushButton", None))
Project.pyw file.
from project import *
import sys
class project(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.pushButton_2, QtCore.SIGNAL('clicked()'), self.home_page)
QtCore.QObject.connect(self.ui.pushButton_3, QtCore.SIGNAL('clicked()'), self.down_page)
def home_page(self):
self.ui.stackedWidget.setCurrentIndex(0)
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL('clicked()'), self.output)
def down_page(self):
self.ui.stackedWidget.setCurrentIndex(1)
def output(self):
print(self.ui.lineEdit.text())
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = project()
myapp.show()
sys.exit(app.exec_())
Any sugestions?
Thanks!!

How to connect 2 windows using pyqtSignal in PyQt?

I create simple application using 2 windows, and i want connect it with button using pyqtsignal, but when i run the application display information "TypeError: pyqtSignal must be bound to a QObject, not 'Form1'":
from PyQt4 import QtCore, QtGui
import form2
import sys
from functools import partial
from PyQt4.QtCore import pyqtSignal
from lib2to3.fixer_util import String
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Form1(QtGui.QMainWindow):
Form2Signal = pyqtSignal(str, str)
def __init__(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(319, 147)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.bt_form1 = QtGui.QPushButton(self.centralwidget)
self.bt_form1.setGeometry(QtCore.QRect(110, 30, 101, 41))
self.bt_form1.setObjectName(_fromUtf8("bt_form1"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 319, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
self.bt_form1.clicked.connect(self.Form2Signal.emit(String))
MainWindow.setWindowTitle(_translate("MainWindow", "Form_1", None))
self.bt_form1.setText(_translate("MainWindow", "FORM_1", None))
class Form2(QtGui.QMainWindow):
Form1Signal = pyqtSignal(str, str)
def __init__(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(319, 147)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.bt_form2 = QtGui.QPushButton(self.centralwidget)
self.bt_form2.setGeometry(QtCore.QRect(110, 30, 101, 41))
self.bt_form2.setObjectName(_fromUtf8("bt_form2"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 319, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
self.bt_form2.clicked.connect(self.Form1Signal.emit)
MainWindow.setWindowTitle(_translate("MainWindow", "Form_2", None))
self.bt_form2.setText(_translate("MainWindow", "FORM_2", None))
class MainWidget(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWidget, self).__init__(parent)
self.stack = QtGui.QStackedWidget()
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.stack)
self.form1 = Form1(self)
self.form2 = Form2(self)
self.stack.addWidget(self.form1)
self.stack.addWidget(self.form2)
self.form1.Form2Signal.connect(partial(self.stack.setCurrentWidget,self.form2))
self.form2.Form1Signal.connect(partial(self.stack.setCurrentWidget,self.form1))
self.stack.setCurrentWidget(self.form1)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = MainWidget()
w.show()
app.exec_()
sys.exit()
Add super(Form1, self).__init__() in __init__ method of Form1 class. Do the same for Form2

Categories