課程有改 所以並不是TracePro
Python圖形化介面
QMainWindow主視窗 可以開啟子視窗
Qwiget可以嵌入到其他視窗的QMainWindow
-
%pastebin cellnumber
#貼上指定cell內容網址
-
Python的類別
繼承之後講
class Cake: def __init__(self,color="blue",size=10): self.__color=color self.__size=size self.show() def changecolor(self,color): self.__color=color self.show() def changesize(self,size): self.__size=size self.show() def show(self): print(self.__color,self.__size) John=Cake(size=98,color="yellow") John.changecolor("black") John.show() |
yellow 98
black 98
black 98
-
建立 一個label 跟 多個pushbuttom
RadioButton單選
CheckBox複選
設定名稱 與 字型 大小
pushbuttom設定鍵盤快速鍵
label 文字靠左
儲存成ui檔
-
使用PyQt5模組
導入剛剛的ui檔,製作計算機
pip install PyQt5
import sys from PyQt5 import QtWidgets from PyQt5.uic import loadUi class Mywindow(QtWidgets.QMainWindow): #繼承 QtWidgets.QMainWindow物件 def __init__(self): super().__init__() #super 使用父類別的初始化條件 loadUi("untitled.ui",self) #xml檔 self.cur_text = self.label.text() #把 面板數字 存入 cur_text 變數 self.click_event() def click_event(self): self.pushButton_0.clicked.connect(lambda: self.label_change('0')) self.pushButton_1.clicked.connect(lambda: self.label_change('1')) self.pushButton_2.clicked.connect(lambda: self.label_change('2')) self.pushButton_3.clicked.connect(lambda: self.label_change('3')) self.pushButton_4.clicked.connect(lambda: self.label_change('4')) self.pushButton_5.clicked.connect(lambda: self.label_change('5')) self.pushButton_6.clicked.connect(lambda: self.label_change('6')) self.pushButton_7.clicked.connect(lambda: self.label_change('7')) self.pushButton_8.clicked.connect(lambda: self.label_change('8')) self.pushButton_9.clicked.connect(lambda: self.label_change('9')) self.pushButton_dot.clicked.connect(lambda: self.label_change('.')) self.pushButton_add.clicked.connect(lambda: self.operator('+')) self.pushButton_sub.clicked.connect(lambda: self.operator('-')) self.pushButton_multiply.clicked.connect(lambda: self.operator('*')) self.pushButton_divide.clicked.connect(lambda: self.operator('/')) self.pushButton_equal.clicked.connect(self.equal) self.pushButton_clear.clicked.connect(self.clear) def label_change(self,number): if (self.cur_text=="0") & (number!="."): self.cur_text=number # 觸發事件的數字 存入 cur_text 變數 self.label.setText(self.cur_text) #顯示到label elif self.cur_text in ["+","-","*","/"]: self.cur_text=number self.label.setText(self.cur_text) else: if ("." in self.cur_text) & (number=="."): self.cur_text=self.cur_text else: self.cur_text+=number # 觸發事件的數字 與 cur_text 變數 字串串接 self.label.setText(self.cur_text) #顯示到label def operator(self,op): self.pre_text=self.cur_text self.cur_text=op self.op=op self.label.setText(self.op) def equal(self): if ("." in self.pre_text) | ("." in self.cur_text): n1=float(self.pre_text) n2=float(self.cur_text) else: n1=int(self.pre_text) n2=int(self.cur_text) if self.op=="+": ans=n1+n2 elif self.op=="-": ans=n1-n2 elif self.op=="*": ans=n1*n2 elif self.op=="/": ans=n1/n2 self.cur_text=str(ans) self.label.setText(self.cur_text) def clear(self): self.pre_text="0" self.cur_text="0" self.label.setText("0") if __name__=="__main__": app=QtWidgets.QApplication(sys.argv) # window=Mywindow() window.show() # show是定義在QtWidgets.QMainWindow裡面的方法 app.exec_() # del app # |
忘記設計不能除以零
-
開啟檔案
QtWidgets.QFileDialog.getOpenFileName(self,"視窗名稱","預設路徑","可接受格式")
import sys from PyQt5 import QtWidgets class Mywindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() #super 使用父類別的初始化條件 self.setGeometry(50,50,1200,800) self.myButton=QtWidgets.QPushButton("Open File",self) self.myButton.setGeometry(100,100,100,50) self.myButton.clicked.connect(self.get_url) #不要括號 def get_url(self): url=QtWidgets.QFileDialog.getOpenFileName(self,"select","/","ALL Files (*);;TXT (*.txt);;CSV Files (*.csv)") print(url) with open(url[0],'r') as f: data=f.read() print(data) if __name__=="__main__": app=QtWidgets.QApplication(sys.argv) # window=Mywindow() window.show() # show是定義在QtWidgets.QMainWindow裡面的方法 app.exec_() # del app # |