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. 将窗口标题更新为 CSCI237 - 课程注册
  2. 更改登录按钮颜色。提示:使用十六进制颜色代码
  3. 更新代码以在单击登录按钮时打印“Hello –username-”。
  4. 添加一个名为 Logout 的新按钮,单击该按钮会打印“再见”。
4

1 回答 1

0

这是您的代码,并进行了一些重构:

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()

夫妻建议:

  • 尽量避免使用全局变量
  • 使用 pyqt 布局而不是硬编码间距/大小是一个不错的方法
于 2018-04-03T19:57:01.157 回答