立即学习: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)运行后