文章目录
1.python库汇总
2. 变量
- 全局变量和局部变量
用global字符来标志全局变量。
a=1
b=2
def disply():
print(a)
def change():
a=b #在change()方法中创建的一个新的局部变量a
print(a)
change()
disply()
>>>2
>>>1
a=1
b=2
def disply():
print(a)
def change():
global a #表示这个a是全局变量里的a
a=b
print(a)
change()
disply()
>>>2
>>>2
- Tuple,List,Dic
dic = {}
tuple = ()
list = []
3. print()
print("\t",end='')
#加上end='',print()以空格结尾,否则,print()以换行结尾
#只有python3有用,python2没用
4. Python 生成 .exe文件
pyinstaller -p dir Analysis_main.py
pyinstaller app.py
pyinstaller -F -w main_btn.py # 生成的程序不带doc黑窗 (-w就是取消窗口)
生成.exe文件后,需要把配置文件(.ini/.config)添加好,这些不会自动生成。
5. 路径读取
获取当前执行文件.py文件的路径 。这种方式生成的.exe文件无法在task schedule中运行,获取路径都是C:\WINDOWS\system32(task schedule的运行路径)
os.getcwd() # 返回执行文件的父级目录
os.listdir() # 得到当前路径下的文件和文件夹名字的列表
root_path = os.path.abspath( _ _ file_ _)
root_path = sys.path[0] # 返回执行文件的父级目录
生成.exe后获取.exe文件的路径。该方式可以在task schedule中可运行
if getattr(sys, 'frozen', False):
root_path = os.path.dirname(sys.executable) # 获取.exe文件的路径
else:
root_path = os.path.dirname(__file__) # 获取.py文件的路径
6. selenium 仿真select
7. 多线程使用
def time_Fun(self, project, station, machine, item):
count = 1
while True:
print(count)
count += 1
time.sleep(3)
# 开启额外的线程
monitor = Thread(target=button_form.time_Fun, args=(project, station, machine, item), daemon=True)
# target=button_form.time_Fun() 这样写,target = 该方法的返回值,而不是一个方法。
# args=(),带入参数,不能写成target=button_form.time_Fun(project, station, machine, item).原因不明
# daemon=True,使得该子线程在主线程结束后也结束。否则主线程结束后该进程任然存在。
# 或者 monitor.setDaemon(True)
monitor.start()
monitor = threading.Timer(1, button_form.time_Fun, [project, station, machine, item, mailto])
# 实现定时执行程序
8. 关于python传参过程中,形参与实参的讨论
参考文件:
python中list作函数形参,如何防止被实参修改
使用了上述所说的 b=a.copy(),生成一个实参备份,并对备份数据进行处理
但是对于 a=list[dic1,dic2,dic3]这种符合类型的变量无效(或者 a=list[list1,lis2,list3]这类复合变量)
进而,参考下面一篇博文 使用deepcopy(),得以解决。
import copy
def function1(a)
b=copy.deepcopy(a)
9. 关于sys.exit(app.exec())的解释
app = QtWidgets.QApplication(sys.argv)
button_form = Button_Form()
button_form.show()
sys.exit(app.exec())
# app.exec() 窗体循环开始,窗体关闭返回 0
# sys.exit()
工作中用到的
- 检测Windows桌面的尺寸
--pip install pywin32
import win32api,win32con
width = win32api.GetSystemMetrics(win32con.SM_CXSCREEN) #屏宽
height = win32api.GetSystemMetrics(win32con.SM_CYSCREEN) #屏高
self.setGeometry(width-150, height-200, 200, 200) # 设置QT界面的绝对位置
- 时间
import time
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))