第六章
GUI:用户图形界面(graphical user interface)
安装easygui:打开cmd命令窗口,输入:pip install easygui
利用msgbox()函数创建一个消息框:
import easygui easygui.msgbox('hello there')
运行后弹出一个消息框:
GUI输出就是消息框
GUI输入:
import easygui user_response = easygui.msgbox('hello there') print(user_response)
鼠标点击了’OK'键,把它返回给变量user_response
msgbox()就是包含一条消息和一个’OK'按钮的对话框
包含多个按钮的对话框:buttonbox()
import easygui flavor = easygui.buttonbox('what is you favorite ice cream flavor?',choices= ['vanilla','chocolate','strawberry']) easygui.msgbox('you picked '+ flavor)
#choices 后面的[]为一个列表
用户点击的按钮的标签就是输入。
选择框:choicebox()
import easygui flavor = easygui.choicebox('what is you favorite ice cream flavor?',choices= ['vanilla','chocolate','strawberry']) easygui.msgbox('you picked '+ flavor)
输入框:enterbox()
import easygui flavor = easygui.enterbox('what is you favorite ice cream flavor?') easygui.msgbox('you picked '+ flavor)
默认输入:可以在enterbox()函数内指定默认输入的内容
import easygui flavor = easygui.enterbox('what is you favorite ice cream flavor?',default='vanilla') easygui.msgbox('you picked '+ flavor)
输入数据:
方法一:
先通过输入框得到一个字符串,然后通过类型变换得到一个数
方法二:
整数框:integerbox()
但只能输入整数(可以对输入设置一个上界和下界),不能输入浮点数
第七章
比较操作符:
<、>、!= (<>)、==
python可以直接把两个大于和小于操作符串在一起完成一个范围测试:
if 8<age<12 if 8<=age<=12
and就是c中的与(&&),可以将多个条件结合在一起
or是c中的或(||),满足任意一个条件则执行
not表示非、取反
第八章
计数循环
for looper in [1,2,4,5]: print("hello")
looper从1开始,每对应一个值就输出一个hello(当looper为3时,列表中没有3,不输出)
死循环时,按下CTRL+C能结束程序。
游戏和图形程序通常都在一个循环中运行。这些程序需要不断从鼠标、键盘或游戏控制器得到输入,然后处理这个输入,并更新屏幕。
中括号[ ]是列表
调用range()函数:
会创建一个列表,包含范围内的所有数(range()包含左边界,不包含右边界)
range()可以缩写range(0,5)和range(5)一样
字符串就像一个字符列表!
for looper in 'hi there': print(looper)
改变步长计数:
for looper in range(1,10,2): print(looper)
#增加第三个变量2,每次加2而不是加1
反向计数
for looper in range(10,1,-1): print(looper)
倒计时的定时器程序:
调入 time库
import time for i in range(10,0,-1): print(i) time.sleep(1) #等待1秒 print("blast off!")
没有数字的计数:
for i in ['a','b','c']: print (i,'is the coolext guy ever!')
while循环:
print('type 3 to continue') someinput = int(input()) while someinput == 3: print('thank you for the 3') print("type 3 to continue") someinput = int(input()) print('it is not 3')
continue 和 break和c中一样
第九章 注释
注释:用井号键#,也可以用三重引号当作注释