现在有一个编好的GUI界面窗口,上面有几个按钮,需要按下去就能打开相应的软件。
有没有人知道怎样用GUI界面的按钮打开软件啊
刚学python~~
可以使用Python的subprocess模块来打开软件。具体的代码实现如下:
import subprocess
from tkinter import *
def open_notepad():
subprocess.Popen("notepad.exe")
def open_calc():
subprocess.Popen("calc.exe")
root = Tk()
notepad_button = Button(root, text="打开记事本", command=open_notepad)
notepad_button.pack()
calc_button = Button(root, text="打开计算器", command=open_calc)
calc_button.pack()
root.mainloop()
上述代码中,我们使用了subprocess模块的Popen方法来打开记事本和计算器。在GUI界面中,我们使用Button组件来创建按钮,并通过command参数将按钮与相应的打开软件的函数进行绑定。
在点击按钮时,相应的函数会被调用,从而打开对应的软件。
为了便于有手就会,我们采用比较暴力简单的手段学习。如果我们要在这个py文件,可以执行另一个py文件怎么办?来个简单的例子:
比如说我这个文件为2.py,我想在这个文件里执行1.py,就如下代码即可:
import os
str = ('python 1.py')
p = os.system(str)
if p == 0:
print('可执行')
else:
print('不可执行')
那么我们同样道理,正确登录才能执行我们的的某个功能代码,我们就把上面那段代码加到原来的登录代码就能正确执行4.py:
# coding=gbk
"""
作者:川川
公众号:玩转大数据
@时间 : 2022/2/10 12:33
群:428335755
输入正确账号和密码方可执行函数
"""
from gooey import Gooey, GooeyParser
import os
@Gooey(
richtext_controls=True, # 打开终端对颜色支持
program_name="疲劳驾驶检测", # 程序名称
encoding="utf-8", # 设置编码格式,打包的时候遇到问题
progress_regex=r"^progress: (\d+)%$" # 正则,用于模式化运行时进度信息
)
def start():
parser = GooeyParser(description="————川川菜鸟制作")
parser.add_argument('username', widget="TextField")
parser.add_argument('password', widget="TextField")
args = parser.parse_args()
return args
if __name__ == '__main__':
args = start()
user = args.username
password = args.password
if user == '川川' and password == '123456':
print('正确,可以开始使用')
str = ('python 2.py')
p = os.system(str)
if p == 0:
print('可执行')
else:
print('不可执行')
# 检测是哪里不对
if user != '川川':
print('用户名错误')
if password != '123456':
print('密码错误')
else:
print('账号或者密码不正确,请确保为本人在使用,请重新输入账号密码')
假设4.py为任意一个正确的代码如下:
#HappyBirthday
import turtle
import time
def move(angle,length):
turtle.penup()
turtle.seth(angle)
turtle.fd(length)
#prepare
turtle.setup(1000,400,100,100)
turtle.penup()
turtle.fd(-350)
turtle.seth(90)
turtle.fd(50)
turtle.pendown()
turtle.pensize(10)
turtle.pencolor("green")
turtle.seth(0)
turtle.hideturtle()
turtle.speed(5)
#呀
turtle.fd(100)
#生
turtle.pencolor("green")
turtle.circle(50,90)
turtle.circle(50,-30)
turtle.seth(0)
turtle.fd(100)
turtle.fd(-50)
turtle.left(90)
turtle.fd(30)
turtle.fd(-60)
turtle.left(90)
turtle.fd(50)
turtle.fd(-100)
turtle.fd(50)
turtle.left(90)
turtle.fd(50)
turtle.right(90)
turtle.fd(60)
turtle.fd(-120)
#日
turtle.penup()
turtle.fd(-30)
turtle.pendown()
turtle.seth(90)
turtle.fd(100)
turtle.seth(0)
turtle.fd(70)
turtle.seth(-90)
turtle.fd(50)
turtle.seth(180)
turtle.fd(70)
turtle.seth(-90)
turtle.fd(50)
turtle.seth(0)
turtle.fd(70)
turtle.seth(90)
turtle.fd(50)
#移动
move(0,30)
#快
turtle.pensize(8)
turtle.circle(30,15)
turtle.pendown()
turtle.circle(30,60)
turtle.penup()
turtle.seth(0)
turtle.fd(13)
turtle.seth(90)
turtle.pendown()
turtle.fd(40)
turtle.fd(-50)
turtle.penup()
turtle.seth(0)
turtle.fd(13)
turtle.pendown()
turtle.seth(-180)
turtle.circle(20,-90)
turtle.circle(20,90)
turtle.penup()
turtle.fd(13)
turtle.pendown()
turtle.seth(-90)
turtle.fd(60)
move(0,40)
move(90,80)
turtle.pendown()
turtle.seth(0)
turtle.fd(30)
turtle.seth(90)
turtle.fd(30)
turtle.fd(-30)
turtle.seth(0)
turtle.fd(20)
turtle.seth(-90)
turtle.fd(35)
turtle.seth(0)
turtle.fd(10)
turtle.fd(-30)
turtle.seth(90)
turtle.fd(35)
turtle.fd(-35)
turtle.seth(0)
turtle.fd(-25)
move(-90,50)
move(180,25)
turtle.pendown()
turtle.seth(0)
turtle.penup()
turtle.circle(50,20)
turtle.pendown()
turtle.circle(50,70)
turtle.seth(-90)
turtle.circle(50,60)
#移动
move(0,50)
move(90,45)
#乐
turtle.pensize(10)
turtle.pendown()
turtle.fd(40)
turtle.seth(0)
turtle.circle(50,60)
turtle.circle(50,-25)
move(-90,15)
turtle.pendown()
turtle.fd(30)
turtle.seth(0)
turtle.fd(-25)
turtle.fd(65)
turtle.fd(-40)
turtle.seth(-90)
turtle.fd(60)
turtle.seth(135)
turtle.fd(20)
move(135,10)
turtle.pendown()
turtle.seth(-135)
turtle.fd(20)
move(0,70)
turtle.pendown()
turtle.seth(135)
turtle.fd(20)
登录:
点击start执行:
最后送上官方文档:
https://github.com/chriskiehl/Gooey