0
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys 
from PyQt4 import QtGui

global username
username = " "

class Home(QtGui.QWidget):

def __init__(self):
    super(Home, self).__init__()

    self.initUI()

def initUI(self):
    font   = QtGui.QFont("Arial",10,QtGui.QFont.Bold,False)    

    username = QtGui.QLabel('Username',self)
    username.move(10,40)
    username.setFont(font);

    usernameEdit = QtGui.QLineEdit(self)
    usernameEdit.move(100,35)
    usernameEdit.textChanged[str].connect(self.onChangedusername)
    usernameEdit.setFocus()

    btn = QtGui.QPushButton('Login', self)
    btn.move(10, 80)
    btn.setFixedWidth(130)
    btn.setFixedHeight(50)     
    btn.setStyleSheet("background-color: #FF0000") #red color
    btn.clicked.connect(self.begin)  
    btn.setFont(font)

    self.setGeometry(5, 30, 600, 300)
    self.setWindowTitle('CSCI237')   #Update window title to CSCI237
    self.show()

def onChangedusername(self, text):
    global username
    username = str(text)

def begin(self):
    print username

def main():
app = QtGui.QApplication(sys.argv)
ex = Home()
sys.exit(app.exec_())



if __name__ == '__main__':
main()
  1. Update the window title to CSCI237 - Course Registration
  2. Change the Login button color. Hint: Use hex color codes
  3. Update the code to print ‘Hello –username-’ when the Login button is clicked.
  4. Add a new button called Logout that prints ‘Goodbye’ when clicked.
mahal
  • 1

1 Answers1

0

Here's your code with a little bit of refactoring:

import sys

from PyQt4.Qt import *  # noqa


class Home(QWidget):

    def __init__(self):
        super(Home, self).__init__()
        self.init_ui()

    def init_ui(self):
        font = QFont("Arial", 10, QFont.Bold, False)

        # Username info
        username_label = QLabel('Username', self)
        username_label.setFont(font)
        self.username_edit = QLineEdit(self)
        self.username_edit.setFocus()

        # Login/Logout buttons
        login_button = QPushButton('Login', self)
        login_button.setStyleSheet("background-color: #FF0000")
        login_button.clicked.connect(self.on_login_button)
        login_button.setFont(font)
        logout_button = QPushButton('Logout', self)
        logout_button.setStyleSheet("background-color: #00FF00")
        logout_button.clicked.connect(self.on_logout_button)
        logout_button.setFont(font)

        # Layout arrangements
        hbox_layout1 = QHBoxLayout()
        hbox_layout1.addWidget(username_label)
        hbox_layout1.addWidget(self.username_edit)
        hbox_layout2 = QHBoxLayout()
        hbox_layout2.addWidget(login_button)
        hbox_layout2.addWidget(logout_button)
        vbox_layout = QVBoxLayout()
        vbox_layout.addLayout(hbox_layout1)
        vbox_layout.addLayout(hbox_layout2)
        self.setLayout(vbox_layout)

        self.setWindowTitle('CSCI237')
        self.show()

    def on_login_button(self):
        username = str(self.username_edit.text())
        if username.strip() == "":
            print("Username is empty!")
        else:
            print("Hello {}".format(username))

    def on_logout_button(self):
        print("GoodBye {}!".format(self.username_edit.text()))
        self.close()


def main():
    app = QApplication(sys.argv)
    ex = Home()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Couple advices:

  • Try avoid using global variables as much as possible
  • Using pyqt layouts instead hardcoded spacing/sizes is a good way to go
BPL
  • 9,632
  • 9
  • 59
  • 117
  • how can i attached a log out button on my original code? – mahal Apr 05 '18 at 17:37
  • @mahal what part of my code is not clear to you? Apart from showing you how to create the log out button is also answering your other questions – BPL Apr 05 '18 at 21:11
  • i need the original code the one that i attached on my question 3 and 4. – mahal Apr 06 '18 at 18:08