立即學習:https://edu.csdn.net/course/play/19711/343123?utm_source=blogtoedu
1.graphics:圖形界面組件的繪制,利用的是坐標的定位來對各個組件進行相對地位置布局
?
2.graphics與thinkter的區別
1)窗口的創建上:
win = graphics.GraphWin("四則運算",#定標題義窗口的700,230#定義窗體的大小尺寸)
2)組件的顯示上,前者用draw后者用pack或者其他的布局函數
graphics.Text(graphics.Point(80,50),#文本框開始繪制的坐標,一般是文本框的左上角點為開始繪制點"計算數字一:"#文本框標題).draw(win)#用于表示在窗口win中顯示,用draw進行顯示
3)設置背景色文本信息上
entry1.setFill("white")#給輸入框設置底色entry1.setText("0.0")#給輸入框設置初始值entry1.draw(win)#輸入框的顯示
?
3.完整代碼
import graphicsdef main():#-------------------創建窗體---------------------win = graphics.GraphWin("四則運算",#定標題義窗口的700,230#定義窗體的大小尺寸)#-------------------創建文本框1----------------graphics.Text(graphics.Point(80,50),#文本框開始繪制的坐標,一般是文本框的左上角點為開始繪制點"計算數字一:"#文本框標題).draw(win)#用于表示在窗口win中顯示,用draw進行顯示# -------------------創建第一個輸入框----------------entry1 = graphics.Entry(graphics.Point(180,50),#設置起始繪制坐標點8)#設置輸入框的寬度entry1.setFill("white")#給輸入框設置底色entry1.setText("0.0")#給輸入框設置初始值entry1.draw(win)#輸入框的顯示#-------------------創建文本框2----------------graphics.Text(graphics.Point(300,50),#文本框開始繪制的坐標,一般是文本框的左上角點為開始繪制點"計算數字二:"#文本框標題).draw(win)#用于表示在窗口win中顯示,用draw進行顯示# -------------------創建第二個輸入框----------------entry2 = graphics.Entry(graphics.Point(400,50),#設置起始繪制坐標點8)#設置輸入框的寬度entry2.setFill("white")#給輸入框設置底色entry2.setText("0.0")#給輸入框設置初始值entry2.draw(win)#輸入框的顯示#---------------------創建顯示四則計算的文本框graphics.Text(graphics.Point(80,100),"加法運算:").draw(win)add_result_entry = graphics.Entry(graphics.Point(180,100),8)add_result_entry.setFill("white")add_result_entry.setText("0.0")add_result_entry.draw(win)graphics.Text(graphics.Point(300,100),"減法運算:").draw(win)sub_result_entry = graphics.Entry(graphics.Point(400,100),8)sub_result_entry.setFill("white")sub_result_entry.setText("0.0")sub_result_entry.draw(win)graphics.Text(graphics.Point(80,150),"乘法運算:").draw(win)mul_result_entry = graphics.Entry(graphics.Point(180,150),8)mul_result_entry.setFill("white")mul_result_entry.setText("0.0")mul_result_entry.draw(win)graphics.Text(graphics.Point(300,150),"除法運算:").draw(win)div_result_entry = graphics.Entry(graphics.Point(400,150),8)div_result_entry.setFill("white")div_result_entry.setText("0.0")div_result_entry.draw(win)graphics.Text(graphics.Point(180,200),"注:第二個數不可以是0,因為除數不可以為0").draw(win)#--------------等待事件執行--------------------win.getMouse()#當輸入完成后單擊鼠標就會自動進行計算#-----------------------進行四則計算并且獲得顯示結果-------------------add_result = eval(entry1.getText()) + eval(entry2.getText())sub_result = eval(entry1.getText()) - eval(entry2.getText())mul_result = eval(entry1.getText()) * eval(entry2.getText())div_result = eval(entry1.getText()) / eval(entry2.getText())add_result_entry.setText(add_result)sub_result_entry.setText(sub_result)mul_result_entry.setText(mul_result)div_result_entry.setText(div_result)win.mainloop()#窗體的顯示if __name__ == '__main__':main()
4.運行結果
1)運行前
2)運行后
?