tkinter를 이용하여 main window없이 파일열기 대화상자만 사용하기

안녕하세요?

별도의 GUI환경을 만들지않고 파일열기 대화상자만을 이용해서 사용자가 선택한 파일정보(path와 filename)만을 주 프로그램으로 전달하려고 합니다.

스텍오버플로우 같은데서 몇가지 관련 내용을 찾아보긴 했는데 그나마 비슷한 내용은 아래내용 같습니다
https://stackoverflow.com/questions/51595372/tkinter-filedialog-askopenfilename-window-wont-close-in-python-3

이를 참조하여 아래의 코드를 실행해보았습니다.

import tkinter
import csv
from tkinter import filedialog

root = tkinter.Tk()
root.wm_withdraw() # this completely hides the root window
# root.iconify() # this will move the root window to a minimized icon.

def character_mentions():
    filename = filedialog.askopenfilename()
    with open(filename, 'r') as infile:
        reader = csv.reader(infile)
        # dict_of_mentions = {rows[0]:rows[1] for rows in reader}
    # print(dict_of_mentions)
    print(reader)
    root.destroy()

character_mentions()

root.mainloop()

내용상 메인창을 감춰뒀다가 filedialog가 실행되고 나서 닫는 방식으로 이해되는데, spyder에서 테스트를 해보면 consol이 멈추지 않고 계속 돌아갑니다. 메시지상으로는 mainloop가 root.destroy()에 의해 종료되지 않고 계속 돌아가는 것 같습니다.

runfile('C:/Users/.../Documents/untitled1.py', wdir='C:/Users/.../Documents/')
<_csv.reader object at 0x00000196B7A89EE0>
Traceback (most recent call last):
File "C:\Users...\Documents\untitled1.py", line 20, in
root.mainloop()
File "C:\ProgramData\Anaconda3\lib\tkinter__init__.py", line 1420, in mainloop
self.tk.mainloop(n)
KeyboardInterrupt

어떻게 해야 tkinter 관련 프로그램을 종료하고 파일 정보를 메인 프로그램으로 넘길 수 있을까요?

미리감사합니다.

dokeun98 1395

2021년 8월 2일 3:14 오후

목록으로
1개의 답변이 있습니다. 1 / 1 Page

파일 이름이 test.py 라면..

python test.py 대신 pythonw test.py 로 실행해 보세요.

박응용

2021년 8월 2일 5:58 오후

감사합니다. pythonw로는 spyder console에서 무한히 돌아가는 문제가 해결되었습니다. - dokeun98님, 2021년 8월 3일 9:29 오전 추천 , 대댓글