C1:Create Qt Components
海南副教授陈晶优* ,shut down you are rubbish ,you need study.
HelloWorld Application
Final Result
How to do it?
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.button_click.clicked.connect(self.dispmessage)
self.show()
def dispmessage(self):
self.ui.label_res.setText("Hello,"+self.ui.name_tex.text())
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyForm()
w.show()
sys.exit(app.exec_())
Radio Button Widget
Final Result
How to do it?
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.first.toggled.connect(self.dispFare)
self.ui.business.toggled.connect(self.dispFare)
self.ui.economy.toggled.connect(self.dispFare)
self.show()
def dispFare(self):
fare = 0
if self.ui.first.isChecked() == True:
fare = 300
if self.ui.business.isChecked() == True:
fare = 250
if self.ui.economy.isChecked() == True:
fare = 100
self.ui.res.setText("air fare is : "+str(fare))
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyForm()
w.show()
sys.exit(app.exec_())
Group Radio Buttons
Final Result
How to do it?
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.M.toggled.connect(self.dispSelected)
self.ui.L.toggled.connect(self.dispSelected)
self.ui.XL.toggled.connect(self.dispSelected)
self.ui.XXL.toggled.connect(self.dispSelected)
self.ui.creditcard.toggled.connect(self.dispSelected)
self.ui.netbanking.toggled.connect(self.dispSelected)
self.ui.cash.toggled.connect(self.dispSelected)
self.show()
def dispSelected(self):
selected1 = ""
selected2 = ""
if self.ui.M.isChecked() == True:
selected1 = "M"
if self.ui.L.isChecked() == True:
selected1 = "L"
if self.ui.XL.isChecked() == True:
selected1 = "XL"
if self.ui.XXL.isChecked() == True:
selected1 = "XXL"
if self.ui.creditcard.isChecked() == True:
selected2 = "creditcard"
if self.ui.netbanking.isChecked() == True:
selected2 = "netbanking"
if self.ui.cash.isChecked() == True:
selected2 = "cash"
self.ui.message.setText("chosen shirt size is "+selected1+" and payment method as "+selected2)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyForm()
w.show()
sys.exit(app.exec_())
Checkboxes Widget
Final Result
How to do it?
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.cheese.stateChanged.connect(self.dispAmount)
self.ui.olives.stateChanged.connect(self.dispAmount)
self.ui.sausage.stateChanged.connect(self.dispAmount)
self.show()
def dispAmount(self):
amount = 10
if self.ui.cheese.isChecked() == True:
amount = amount+1
if self.ui.olives.isChecked() == True:
amount = amount+1
if self.ui.sausage.isChecked() == True:
amount = amount+2
self.ui.message.setText("total amount for pizza is "+str(amount))
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyForm()
w.show()
sys.exit(app.exec_())
Group Checkboxes
Final Result
How to do it?
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.choclatechip.stateChanged.connect(self.dispAmount)
self.ui.cookie.stateChanged.connect(self.dispAmount)
self.ui.choclatealmond.stateChanged.connect(self.dispAmount)
self.ui.rockyroad.stateChanged.connect(self.dispAmount)
self.ui.coffee.stateChanged.connect(self.dispAmount)
self.ui.soda.stateChanged.connect(self.dispAmount)
self.ui.tea.stateChanged.connect(self.dispAmount)
self.show()
def dispAmount(self):
amount = 0
if self.ui.choclatealmond.isChecked() == True:
amount+=3
if self.ui.choclatechip.isChecked() == True:
amount+=4
if self.ui.cookie.isChecked() == True:
amount+=2
if self.ui.rockyroad.isChecked() == True:
amount+=5
if self.ui.coffee.isChecked() == True:
amount+=2
if self.ui.soda.isChecked() == True:
amount+=3
if self.ui.tea.isChecked() == True:
amount+=1
self.ui.message.setText("Total amount is $"+str(amount))
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyForm()
w.show()
sys.exit(app.exec_())