codehaus


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Pyqt5 help


I was making a pyqt5 project and I ran into a problem. I want a button in
one window to add a label in another window when clicked upon. But that is
not happening. Now the no. Of labels depend upon something, so I decided to
use a for loop which is not working. Attaching the code below. Can you help?
-------------- next part --------------
from PyQt5 import QtWidgets, QtCore, QtGui
from datetime import *
from PyQt5.QtWidgets import QApplication, QMainWindow, QLineEdit
import sys

class mywindow(QMainWindow):
    def __init__(self):
        super(mywindow, self).__init__()
        self.setGeometry(100, 100, 700, 600)
        self.setWindowTitle("The HBT App")
       
        self.initUi()
    def initUi(self):
        self.img_label1 = QtWidgets.QLabel(self)
        self.img_label1.setGeometry(QtCore.QRect(235, 0, 470, 270))
        self.img_label1.setPixmap(QtGui.QPixmap("birthday cake.jpg"))
        self.img_label1.setScaledContents(True)
        self.img_label1 = QtWidgets.QLabel(self)
        self.img_label1.setGeometry(QtCore.QRect(0, 0, 240, 270))
        self.img_label1.setPixmap(QtGui.QPixmap("whitewash.jpg"))
        self.img_label1.setScaledContents(True)
        self.img_label2 = QtWidgets.QLabel(self)
        self.img_label2.setPixmap(QtGui.QPixmap("blackline.jpg"))
        self.img_label2.setGeometry(QtCore.QRect(100, 298, 605, 3))

        self.Label = QtWidgets.QLabel(self)
        self.Label.setText("Upcoming")
        
        self.Label.setFont(QtGui.QFont("Arial", 14))
        self.Label.setGeometry(QtCore.QRect(10, 280, 101, 31))
        
        self.button = QtWidgets.QPushButton(self)
        self.button.setText("Add..")
        self.button.setGeometry(QtCore.QRect(590, 510, 80, 60))
        self.button.clicked.connect(self.clicked)
        self.button.setFont(QtGui.QFont("Arial", 18))
     
        
        self.n = {}






    def clicked(self):
        self.NewWindow = QtWidgets.QMainWindow()
        self.NewWindow.setGeometry(100, 100, 500, 500)
        self.NewWindow.setWindowTitle("Add friends")
        self.FriendLabel = QtWidgets.QLabel(self.NewWindow)
        self.FriendLabel.setText("Add Friends")
        self.FriendLabel.setFont(QtGui.QFont("Comic Sans MS", 18))
        self.FriendLabel.setGeometry(180, 40, 400, 100)

        self.Namelabel = QtWidgets.QLabel(self.NewWindow)
        self.Namelabel.setText("Name : ")
        self.Namelabel.setGeometry(QtCore.QRect(80, 170, 90, 75))
        self.Namelabel.setFont(QtGui.QFont("Comic Sans MS", 14))
        self.Namefield = QLineEdit(self.NewWindow)
        self.Namefield.setGeometry(QtCore.QRect(160, 190, 250, 35))
        

        self.Datelabel = QtWidgets.QLabel(self.NewWindow)
        self.Datelabel.setText("Birthday:  ")
        self.Datelabel.setGeometry(QtCore.QRect(62, 240, 90, 75))
        self.Datelabel.setFont(QtGui.QFont("Comic Sans MS", 14))
        self.Datefield = QLineEdit(self.NewWindow)
        self.Datefield.setGeometry(QtCore.QRect(160, 260, 250, 35))

        self.Save = QtWidgets.QPushButton(self.NewWindow)
        self.Save.setText("Save")
        self.Save.setFont(QtGui.QFont("Comic Sans MS", 14))
        self.Save.setGeometry(QtCore.QRect(70, 360, 90, 50))
        self.Save.clicked.connect(self.save)

        self.Cancel = QtWidgets.QPushButton(self.NewWindow)
        self.Cancel.setText("Cancel")
        self.Cancel.setFont(QtGui.QFont("Comic Sans MS", 14))
        self.Cancel.setGeometry(QtCore.QRect(360, 360, 90, 50))
        self.Cancel.clicked.connect(self.cancel)
        self.NewWindow.show()

    def save(self):
        self.friend_name = self.Namefield.text()
        self.friend_date = self.Datefield.text()
        self.Namefield.clear()
        self.Datefield.clear()
        self.n[self.friend_name] = self.friend_date
        print(self.n)


        for a in self.n:
            NewLabel = QtWidgets.QLabel(self)
            NewLabel.setText(self.friend_name + "has birthday on " + self.friend_date)
            NewLabel.setGeometry(QtCore.QRect(30, 250, 100, 100))

        
        self.check_date()

    def check_date(self):
        today = datetime.now()
        only_date = today.day
        strdate = datetime.strptime(self.friend_date, "%d/%m").date()
        onlydate = strdate.day
        only_month = today.month
        onlymonth = strdate.month
        if only_month == onlymonth and onlydate == only_date:
            print("Today is ", self.friend_name, "'s birthday.")
    def cancel(self):
        self.Namefield.clear()
        self.Datefield.clear()
        


def window():

    app = QApplication(sys.argv)
    win = mywindow()

    win.show()
    sys.exit(app.exec_())

window()