Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Tkinter를 사용하여 외부 프로그램을 여는 방법은 무엇입니까?


때로는 응용 프로그램을 만드는 동안 외부 프로그램 및 응용 프로그램과 상호 작용해야 합니다. 시스템의 응용 프로그램 및 프로그램과 상호 작용하려면 os를 사용해야 합니다. 파이썬의 모듈.

이 기사에서는 Python의 OS 모듈을 사용하여 외부 프로그램 및 오픈 파일과 상호 작용하는 방법을 볼 것입니다.

먼저 filedialog를 사용하여 선택한 파일을 여는 함수를 정의합니다. 파이썬의 라이브러리. 그런 다음 경로를 인쇄하고 os를 사용하여 파일을 엽니다. 모듈.

예시

# Import the required Libraries
from tkinter import *
from tkinter import filedialog
import os

#Create an instance of tkinter frame
win= Tk()

#Set the geometry for the window or frame
win.geometry("600x400")

#Define a function to open the application

def app():
   file= filedialog.askopenfilename()
   text.config(text= file)
   #Open the program
   os.system('"%s"' %file)

#Create a button
Button(win, text='Click to Open a Program',font=('Poppins bold', 10),
command=app).pack(pady=20)

#Create a Label after button event
text= Label(win, text= "", font= ('Poppins bold', 10))
text.pack(pady=20)

#Keep running the window or frame
win.mainloop()

출력

위의 코드를 실행하면 다음 창이 출력으로 생성됩니다 -

Tkinter를 사용하여 외부 프로그램을 여는 방법은 무엇입니까?

이제 버튼을 클릭하면 프로그램을 열 수 있는 "내 문서" 폴더가 열립니다.