PyQt5設計介面方法
阿新 • • 發佈:2019-02-14
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import random
import time
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(800, 600)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(80, 10, 200, 40))
font = QtGui.QFont()
font.setFamily("楷體")
font.setPointSize(36)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(60, 70, 240, 20))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setTabletTracking(False)
self.label_2.setObjectName("label_2")
self.groupBox = QtWidgets.QGroupBox(Form)
self.groupBox.setEnabled(False)
self.groupBox.setGeometry(QtCore.QRect(40, 100, 270, 280))
self.groupBox.setTitle("")
self.groupBox.setObjectName("groupBox")
self.Button_1 = QtWidgets.QPushButton(self.groupBox)
self.Button_1.setGeometry(QtCore.QRect(40, 50, 32, 32))
self.Button_1.setAutoDefault(True)
self.Button_1.setDefault(False)
self.Button_1.setFlat(False)
self.Button_1.setObjectName("Button_1")
self.Button_2 = QtWidgets.QPushButton(self.groupBox)
self.Button_2.setGeometry(QtCore.QRect(100, 50, 32, 32))
self.Button_2.setAutoDefault(True)
self.Button_2.setDefault(False)
self.Button_2.setFlat(False)
self.Button_2.setObjectName("Button_2")
self.Button_3 = QtWidgets.QPushButton(self.groupBox)
self.Button_3.setGeometry(QtCore.QRect(160, 50, 32, 32))
self.Button_3.setAutoDefault(True)
self.Button_3.setDefault(False)
self.Button_3.setFlat(False)
self.Button_3.setObjectName("Button_3")
self.Button_4 = QtWidgets.QPushButton(self.groupBox)
self.Button_4.setGeometry(QtCore.QRect(40, 110, 32, 32))
self.Button_4.setAutoDefault(True)
self.Button_4.setDefault(False)
self.Button_4.setFlat(False)
self.Button_4.setObjectName("Button_4")
self.Button_5 = QtWidgets.QPushButton(self.groupBox)
self.Button_5.setGeometry(QtCore.QRect(100, 110, 32, 32))
self.Button_5.setAutoDefault(True)
self.Button_5.setDefault(False)
self.Button_5.setFlat(False)
self.Button_5.setObjectName("Button_5")
self.Button_6 = QtWidgets.QPushButton(self.groupBox)
self.Button_6.setGeometry(QtCore.QRect(160, 110, 32, 32))
self.Button_6.setAutoDefault(True)
self.Button_6.setDefault(False)
self.Button_6.setFlat(False)
self.Button_6.setObjectName("Button_6")
self.Button_7 = QtWidgets.QPushButton(self.groupBox)
self.Button_7.setGeometry(QtCore.QRect(40, 170, 32, 32))
self.Button_7.setAutoDefault(True)
self.Button_7.setDefault(False)
self.Button_7.setFlat(False)
self.Button_7.setObjectName("Button_7")
self.Button_8 = QtWidgets.QPushButton(self.groupBox)
self.Button_8.setGeometry(QtCore.QRect(100, 170, 32, 32))
self.Button_8.setAutoDefault(True)
self.Button_8.setDefault(False)
self.Button_8.setFlat(False)
self.Button_8.setObjectName("Button_8")
self.Button_9 = QtWidgets.QPushButton(self.groupBox)
self.Button_9.setGeometry(QtCore.QRect(160, 170, 32, 32))
self.Button_9.setAutoDefault(True)
self.Button_9.setDefault(False)
self.Button_9.setFlat(False)
self.Button_9.setObjectName("Button_9")
self.Button_0 = QtWidgets.QPushButton(self.groupBox)
self.Button_0.setGeometry(QtCore.QRect(40, 230, 32, 32))
self.Button_0.setAutoDefault(True)
self.Button_0.setDefault(False)
self.Button_0.setFlat(False)
self.Button_0.setObjectName("Button_0")
self.Button_start = QtWidgets.QPushButton(self)
self.Button_start.setGeometry(QtCore.QRect(330, 72, 52, 22))
self.Button_start.setObjectName("Button_start")
self.Button_reset = QtWidgets.QPushButton(self.groupBox)
self.Button_reset.setGeometry(QtCore.QRect(210, 100, 52, 22))
self.Button_reset.setObjectName("Button_reset")
self.Button_ok = QtWidgets.QPushButton(self.groupBox)
self.Button_ok.setGeometry(QtCore.QRect(210, 50, 52, 22))
self.Button_ok.setObjectName("Button_ok")
self.table = QtWidgets.QTableWidget(Form)
self.table.setGeometry(QtCore.QRect(330, 100, 420, 400))
self.table.setAutoScroll(True)
self.table.setObjectName("table")
self.table.setColumnCount(4)
self.table.setRowCount(8)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "猜數字遊戲"))
self.cishu = 1
self.max=100
self.min=0
self.label.setText("點開始吧")
self.label_2.setText(_translate("Form", "請猜0~100之間的整數:"))
self.Button_1.setText(_translate("Form", "1"))
self.Button_1.clicked.connect(self.Button1)
self.Button_2.setText(_translate("Form", "2"))
self.Button_2.clicked.connect(self.Button2)
self.Button_3.setText(_translate("Form", "3"))
self.Button_3.clicked.connect(self.Button3)
self.Button_4.setText(_translate("Form", "4"))
self.Button_4.clicked.connect(self.Button4)
self.Button_5.setText(_translate("Form", "5"))
self.Button_5.clicked.connect(self.Button5)
self.Button_6.setText(_translate("Form", "6"))
self.Button_6.clicked.connect(self.Button6)
self.Button_7.setText(_translate("Form", "7"))
self.Button_7.clicked.connect(self.Button7)
self.Button_8.setText(_translate("Form", "8"))
self.Button_8.clicked.connect(self.Button8)
self.Button_9.setText(_translate("Form", "9"))
self.Button_9.clicked.connect(self.Button9)
self.Button_0.setText(_translate("Form", "0"))
self.Button_0.clicked.connect(self.Button0)
self.Button_start.setText(_translate("Form", "開始"))
self.Button_start.clicked.connect(self.start)
self.Button_reset.setText(_translate("Form", "重置"))
self.Button_reset.clicked.connect(self.reset)
self.Button_ok.setText(_translate("Form", "確定"))
self.Button_ok.clicked.connect(self.ok)
def isDeision(self):
if self.label.text()=="太大了!" or self.label.text()=="太小了!" :
return True
else:
return False
def text_show(self, Button_i):
if self.isDeision()==True:
text = Button_i
else:
text = self.label.text() + Button_i
self.label.setText(text)
def Button0(self):
Button_i = "0"
self.text_show(Button_i)
def Button1(self):
Button_i = "1"
self.text_show(Button_i)
def Button2(self):
Button_i = "2"
self.text_show(Button_i)
def Button3(self):
Button_i = "3"
self.text_show(Button_i)
def Button4(self):
Button_i = "4"
self.text_show(Button_i)
def Button5(self):
Button_i = "5"
self.text_show(Button_i)
def Button6(self):
Button_i = "6"
self.text_show(Button_i)
def Button7(self):
Button_i = "7"
self.text_show(Button_i)
def Button8(self):
Button_i = "8"
self.text_show(Button_i)
def Button9(self):
Button_i = "9"
self.text_show(Button_i)
def start(self):
self.time_start = time.time()
self.groupBox.setEnabled(True)
self.Button_reset.setEnabled(True)
self.label.setText("")
self.table.clearContents()
self.label_2.setText("請猜一個0~100之間的整數:")
self.randnum = random.randint(0, 100)
self.min=0
self.max=100
self.cishu=1
def SetButton(self,mm=False):
self.Button_0.setEnabled(mm)
self.Button_1.setEnabled(mm)
self.Button_2.setEnabled(mm)
self.Button_3.setEnabled(mm)
self.Button_4.setEnabled(mm)
self.Button_5.setEnabled(mm)
self.Button_6.setEnabled(mm)
self.Button_7.setEnabled(mm)
self.Button_8.setEnabled(mm)
self.Button_9.setEnabled(mm)
def ok(self):
time_end = time.time()
self.sumtime = time_end - self.time_start
# self.SetButton(False)
Guessold=self.label.text()
if self.cishu < 7:
if int(Guessold) == self.randnum:
self.label.setText("你猜對了!")
elif int(Guessold) > self.randnum:
if int(Guessold)<=100:
self.max=int(Guessold)
self.label.setText("太大了!")
elif int(Guessold) < self.randnum:
self.label.setText("太小了!")
self.min=int(Guessold)
table_data = QtWidgets.QTableWidgetItem(self.label.text())
self.table.setItem(self.cishu - 1, 0, table_data)
table_data1 = QtWidgets.QTableWidgetItem("你猜的數是:%s 第%d步" % (Guessold,self.cishu))
self.table.setItem(self.cishu - 1, 1, table_data1)
self.label_2.setText("請猜一個%s~%s之間的整數:"%(self.min,self.max))
# self.table.setItem(self.i - 1, 2, "jin")
else:
self.label_2.setText("你的機會用光了,一共耗時"+str(int(self.sumtime))+"秒")
self.Button_reset.setEnabled(False)
self.label.setText('你輸了!')
self.cishu+=1
def reset(self):
from PyQt5.QtWidgets import QApplication, QMainWindow
import random
import time
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(800, 600)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(80, 10, 200, 40))
font = QtGui.QFont()
font.setFamily("楷體")
font.setPointSize(36)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(60, 70, 240, 20))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setTabletTracking(False)
self.label_2.setObjectName("label_2")
self.groupBox = QtWidgets.QGroupBox(Form)
self.groupBox.setEnabled(False)
self.groupBox.setGeometry(QtCore.QRect(40, 100, 270, 280))
self.groupBox.setTitle("")
self.groupBox.setObjectName("groupBox")
self.Button_1 = QtWidgets.QPushButton(self.groupBox)
self.Button_1.setGeometry(QtCore.QRect(40, 50, 32, 32))
self.Button_1.setAutoDefault(True)
self.Button_1.setDefault(False)
self.Button_1.setFlat(False)
self.Button_1.setObjectName("Button_1")
self.Button_2 = QtWidgets.QPushButton(self.groupBox)
self.Button_2.setGeometry(QtCore.QRect(100, 50, 32, 32))
self.Button_2.setAutoDefault(True)
self.Button_2.setDefault(False)
self.Button_2.setFlat(False)
self.Button_2.setObjectName("Button_2")
self.Button_3 = QtWidgets.QPushButton(self.groupBox)
self.Button_3.setGeometry(QtCore.QRect(160, 50, 32, 32))
self.Button_3.setAutoDefault(True)
self.Button_3.setDefault(False)
self.Button_3.setFlat(False)
self.Button_3.setObjectName("Button_3")
self.Button_4 = QtWidgets.QPushButton(self.groupBox)
self.Button_4.setGeometry(QtCore.QRect(40, 110, 32, 32))
self.Button_4.setAutoDefault(True)
self.Button_4.setDefault(False)
self.Button_4.setFlat(False)
self.Button_4.setObjectName("Button_4")
self.Button_5 = QtWidgets.QPushButton(self.groupBox)
self.Button_5.setGeometry(QtCore.QRect(100, 110, 32, 32))
self.Button_5.setAutoDefault(True)
self.Button_5.setDefault(False)
self.Button_5.setFlat(False)
self.Button_5.setObjectName("Button_5")
self.Button_6 = QtWidgets.QPushButton(self.groupBox)
self.Button_6.setGeometry(QtCore.QRect(160, 110, 32, 32))
self.Button_6.setAutoDefault(True)
self.Button_6.setDefault(False)
self.Button_6.setFlat(False)
self.Button_6.setObjectName("Button_6")
self.Button_7 = QtWidgets.QPushButton(self.groupBox)
self.Button_7.setGeometry(QtCore.QRect(40, 170, 32, 32))
self.Button_7.setAutoDefault(True)
self.Button_7.setDefault(False)
self.Button_7.setFlat(False)
self.Button_7.setObjectName("Button_7")
self.Button_8 = QtWidgets.QPushButton(self.groupBox)
self.Button_8.setGeometry(QtCore.QRect(100, 170, 32, 32))
self.Button_8.setAutoDefault(True)
self.Button_8.setDefault(False)
self.Button_8.setFlat(False)
self.Button_8.setObjectName("Button_8")
self.Button_9 = QtWidgets.QPushButton(self.groupBox)
self.Button_9.setGeometry(QtCore.QRect(160, 170, 32, 32))
self.Button_9.setAutoDefault(True)
self.Button_9.setDefault(False)
self.Button_9.setFlat(False)
self.Button_9.setObjectName("Button_9")
self.Button_0 = QtWidgets.QPushButton(self.groupBox)
self.Button_0.setGeometry(QtCore.QRect(40, 230, 32, 32))
self.Button_0.setAutoDefault(True)
self.Button_0.setDefault(False)
self.Button_0.setFlat(False)
self.Button_0.setObjectName("Button_0")
self.Button_start = QtWidgets.QPushButton(self)
self.Button_start.setGeometry(QtCore.QRect(330, 72, 52, 22))
self.Button_start.setObjectName("Button_start")
self.Button_reset = QtWidgets.QPushButton(self.groupBox)
self.Button_reset.setGeometry(QtCore.QRect(210, 100, 52, 22))
self.Button_reset.setObjectName("Button_reset")
self.Button_ok = QtWidgets.QPushButton(self.groupBox)
self.Button_ok.setGeometry(QtCore.QRect(210, 50, 52, 22))
self.Button_ok.setObjectName("Button_ok")
self.table = QtWidgets.QTableWidget(Form)
self.table.setGeometry(QtCore.QRect(330, 100, 420, 400))
self.table.setAutoScroll(True)
self.table.setObjectName("table")
self.table.setColumnCount(4)
self.table.setRowCount(8)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "猜數字遊戲"))
self.cishu = 1
self.max=100
self.min=0
self.label.setText("點開始吧")
self.label_2.setText(_translate("Form", "請猜0~100之間的整數:"))
self.Button_1.setText(_translate("Form", "1"))
self.Button_1.clicked.connect(self.Button1)
self.Button_2.setText(_translate("Form", "2"))
self.Button_2.clicked.connect(self.Button2)
self.Button_3.setText(_translate("Form", "3"))
self.Button_3.clicked.connect(self.Button3)
self.Button_4.setText(_translate("Form", "4"))
self.Button_4.clicked.connect(self.Button4)
self.Button_5.setText(_translate("Form", "5"))
self.Button_5.clicked.connect(self.Button5)
self.Button_6.setText(_translate("Form", "6"))
self.Button_6.clicked.connect(self.Button6)
self.Button_7.setText(_translate("Form", "7"))
self.Button_7.clicked.connect(self.Button7)
self.Button_8.setText(_translate("Form", "8"))
self.Button_8.clicked.connect(self.Button8)
self.Button_9.setText(_translate("Form", "9"))
self.Button_9.clicked.connect(self.Button9)
self.Button_0.setText(_translate("Form", "0"))
self.Button_0.clicked.connect(self.Button0)
self.Button_start.setText(_translate("Form", "開始"))
self.Button_start.clicked.connect(self.start)
self.Button_reset.setText(_translate("Form", "重置"))
self.Button_reset.clicked.connect(self.reset)
self.Button_ok.setText(_translate("Form", "確定"))
self.Button_ok.clicked.connect(self.ok)
def isDeision(self):
if self.label.text()=="太大了!" or self.label.text()=="太小了!" :
return True
else:
return False
def text_show(self, Button_i):
if self.isDeision()==True:
text = Button_i
else:
text = self.label.text() + Button_i
self.label.setText(text)
def Button0(self):
Button_i = "0"
self.text_show(Button_i)
def Button1(self):
Button_i = "1"
self.text_show(Button_i)
def Button2(self):
Button_i = "2"
self.text_show(Button_i)
def Button3(self):
Button_i = "3"
self.text_show(Button_i)
def Button4(self):
Button_i = "4"
self.text_show(Button_i)
def Button5(self):
Button_i = "5"
self.text_show(Button_i)
def Button6(self):
Button_i = "6"
self.text_show(Button_i)
def Button7(self):
Button_i = "7"
self.text_show(Button_i)
def Button8(self):
Button_i = "8"
self.text_show(Button_i)
def Button9(self):
Button_i = "9"
self.text_show(Button_i)
def start(self):
self.time_start = time.time()
self.groupBox.setEnabled(True)
self.Button_reset.setEnabled(True)
self.label.setText("")
self.table.clearContents()
self.label_2.setText("請猜一個0~100之間的整數:")
self.randnum = random.randint(0, 100)
self.min=0
self.max=100
self.cishu=1
def SetButton(self,mm=False):
self.Button_0.setEnabled(mm)
self.Button_1.setEnabled(mm)
self.Button_2.setEnabled(mm)
self.Button_3.setEnabled(mm)
self.Button_4.setEnabled(mm)
self.Button_5.setEnabled(mm)
self.Button_6.setEnabled(mm)
self.Button_7.setEnabled(mm)
self.Button_8.setEnabled(mm)
self.Button_9.setEnabled(mm)
def ok(self):
time_end = time.time()
self.sumtime = time_end - self.time_start
# self.SetButton(False)
Guessold=self.label.text()
if self.cishu < 7:
if int(Guessold) == self.randnum:
self.label.setText("你猜對了!")
elif int(Guessold) > self.randnum:
if int(Guessold)<=100:
self.max=int(Guessold)
self.label.setText("太大了!")
elif int(Guessold) < self.randnum:
self.label.setText("太小了!")
self.min=int(Guessold)
table_data = QtWidgets.QTableWidgetItem(self.label.text())
self.table.setItem(self.cishu - 1, 0, table_data)
table_data1 = QtWidgets.QTableWidgetItem("你猜的數是:%s 第%d步" % (Guessold,self.cishu))
self.table.setItem(self.cishu - 1, 1, table_data1)
self.label_2.setText("請猜一個%s~%s之間的整數:"%(self.min,self.max))
# self.table.setItem(self.i - 1, 2, "jin")
else:
self.label_2.setText("你的機會用光了,一共耗時"+str(int(self.sumtime))+"秒")
self.Button_reset.setEnabled(False)
self.label.setText('你輸了!')
self.cishu+=1
def reset(self):
self.label.setText("")
將以上檔案儲存為:GuessNumber.py程式,當然,最好先用PyQt5生成.ui再轉換為.py,這樣效率更高。
再寫一個py程式(只是一個骨架):
import GuessNumber
from PyQt5.QtWidgets import QApplication, QWidget
import sys
class MyWindow(QWidget, GuessNumber.Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = mywindow()
win.show()
app.exec_()