Python编程基础与应用 第2版 课习题答案 李方园 第6章答案_第1页
Python编程基础与应用 第2版 课习题答案 李方园 第6章答案_第2页
Python编程基础与应用 第2版 课习题答案 李方园 第6章答案_第3页
Python编程基础与应用 第2版 课习题答案 李方园 第6章答案_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

6.1以例程为例,运行结果如图1所示图1GUI控件包括:窗口、菜单等。6.2参考程序(注意文件名称不能是jpg,且需要放置在当前目录下)importtkinterastkroot=tk.Tk()#增加背景图片photo=tk.PhotoImage(file="bg.png")theLabel=tk.Label(root,text="大家好,才是真的好.",#内容justify=tk.LEFT,#对齐方式image=photo,#加入图片compound=tk.CENTER,#关键:设置为背景图片font=("华文行楷",20),#字体和字号fg="white")#前景色theLabel.pack()tk.mainloop()6.3参考程序(这里是复选框)importtkinterastktop=tk.Tk()CheckVar1=tk.IntVar()CheckVar2=tk.IntVar()CheckVar3=tk.IntVar()C1=tk.Checkbutton(top,text="One",variable=CheckVar1,\onvalue=1,offvalue=0,height=1,\width=20)C2=tk.Checkbutton(top,text="Two",variable=CheckVar2,\onvalue=1,offvalue=0,height=1,\width=20)C3=tk.Checkbutton(top,text="Three",variable=CheckVar3,\onvalue=1,offvalue=0,height=1,\width=20)C1.pack()C2.pack()C3.pack()top.mainloop()6.4参考程序importtkinterasTK#主窗口root=TK.Tk()root.title("Caculator")root.resizable(0,0)root.geometry('320x420')result=TK.StringVar()equation=TK.StringVar()result.set('')equation.set('0')#获得按下的数字或者符号defgetnum(num):temp=equation.get()temp2=result.get()print(temp)print(temp2)iftemp2!='':temp='0'temp2=''result.set(temp2)if(temp=='0'):temp=''temp=temp+numequation.set(temp)print(equation)#按下退格键时,去除最后一个字符defback():temp=equation.get()equation.set(temp[:-1])#按下MC时,清空算式行与结果行defclear():equation.set('0')result.set('')#按下等于号时计算结果defrun():temp=equation.get()temp=temp.replace('x','*')temp=temp.replace('÷','/')print(temp)answer=eval(temp)answer='%.2f'%answerresult.set(str(answer))#结果显示框show_uresult=TK.Label(root,bg='white',fg='black',font=('Arail','15'),bd='0',textvariable=equation,anchor='se')show_dresult=TK.Label(root,bg='white',fg='black',font=('Arail','30'),bd='0',textvariable=result,anchor='se')show_uresult.place(x='10',y='10',width='300',height='50')show_dresult.place(x='10',y='60',width='300',height='50')#按钮#第1行按钮button_7=TK.Button(root,text='7',bg='DarkGray',command=lambda:getnum('7'))button_7.place(x='10',y='150',width='60',height='40')button_8=TK.Button(root,text='8',bg='DarkGray',command=lambda:getnum('8'))button_8.place(x='90',y='150',width='60',height='40')button_9=TK.Button(root,text='9',bg='DarkGray',command=lambda:getnum('9'))button_9.place(x='170',y='150',width='60',height='40')button_multiplication=TK.Button(root,text='X',bg='DarkGray',command=lambda:getnum('x'))button_multiplication.place(x='250',y='260',width='60',height='40')#第2行按钮button_4=TK.Button(root,text='4',bg='DarkGray',command=lambda:getnum('4'))button_4.place(x='10',y='205',width='60',height='40')button_5=TK.Button(root,text='5',bg='DarkGray',command=lambda:getnum('5'))button_5.place(x='90',y='205',width='60',height='40')button_6=TK.Button(root,text='6',bg='DarkGray',command=lambda:getnum('6'))button_6.place(x='170',y='205',width='60',height='40')button_minus=TK.Button(root,text='—',bg='DarkGray',command=lambda:getnum('-'))button_minus.place(x='250',y='205',width='60',height='40')#第3行按钮button_1=TK.Button(root,text='1',bg='DarkGray',command=lambda:getnum('1'))button_1.place(x='10',y='260',width='60',height='40')button_2=TK.Button(root,text='2',bg='DarkGray',command=lambda:getnum('2'))button_2.place(x='90',y='260',width='60',height='40')button_3=TK.Button(root,text='3',bg='DarkGray',command=lambda:getnum('3'))button_3.place(x='170',y='260',width='60',height='40')button_plus=TK.Button(root,text='+',bg='DarkGray',command=lambda:getnum('+'))button_plus.place(x='250',y='150',width='60',height='40')#第4、5按钮button_MC=TK.Button(root,text='C',bg='DarkGray',command=clear)button_MC.place(x='90',y='370',width='60',height='40')button_0=TK.Button(root,text='0',bg='DarkGray',command=lambda:getnum('0'))button_0.place(x='10',y='315',width='140',height='40')button_point=TK.Button(root,text='.',bg='DarkGray',command=lambda:getnum('.'))button_point.place(x='170',y='315',width='60',height='40')button_equal=TK.Button(root,text='=',bg='DarkGray',command=run)button_equal.place(x='170',y='370',width='140',height='40')button_back=TK.Button(root,text='<-',bg='DarkGray',comman

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论