*filedialog (파일 대화창)

 

파일 대화창은 파일을 선택하는 대화창이다. 사용자로부터 파일이나 디렉토리 입력을 받기위해서 아래와 같은 창이 열리는 것을 file dialog 라 한다.

 

from tkinter import filedialog
 
filedialog.asksaveasfilename()
filedialog.asksaveasfile()
filedialog.askopenfilename()
filedialog.askopenfile()
filedialog.askdirectory()
filedialog.askopenfilenames()
filedialog.askopenfiles()

tkinter에는 여러가지 종류의 파일대화창이 있다. 메소드 이름을 보면 스스로를 설명하고 있다. 특히 파일의 이름을 요구하는 것인지 파일 자체를 요구하는 것인지에 차이가 있다.

 

파일의 이름은 그냥 파일의 이름에 대한 정보를 문자열로 반환할 것이고,

 

파일 자체를 요청하면 파일 내용에 접근할 수 있도록 파일객체를 반환한다.

<예제>

import tkinter as tk
import tkinter.filedialog as fd

root = tk.Tk()
root.geometry('320x240')
root.title('Tkinter Test')

frame = tk.Frame(root)
frame.pack()

path1 = fd.askopenfilename(initialdir='/',title="select a file",
                          filetypes =(("Python files","*.py"),
                                      ("txt files","*.txt"),("all files","*.*")))
print(path1)

file1 = fd.askopenfile(initialdir='/',title="select a file",
                          filetypes =(("Python files","*.py"),
                                      ("txt files","*.txt"),("all files","*.*")))

print(file1.readline())

root.mainloop()

예제에서 path1은 선택한 파일의 경로를 받아서 출력한다. file1에는 실제 파일의 참조로 받아와 파일 내용을 한줄 읽는다.

파일 다이어로그

 

출력값


Scrollbars

 

스크롤바는 웹브라우저 같이 긴 내용을 스크롤하면서 읽을 수 있는 기능이다.

 

포함관계를 보면 스크롤을 루트에 고정시키고 리스트를 스크롤에 연결시킨다. 스크롤바의 속성이 모든 위젯에 연결시킬수는 없게 되어 있다. 캔버스나 엔트리,  리스트박스, 텍스트 위젯만 연결이 가능하다.

 

root = tk.Tk()
root.geometry("320x240")

mylabel = tk.Label(root, text='Scrollbars', font="30")
mylabel.pack()

myscroll = tk.Scrollbar(root)
myscroll.pack(side=tk.RIGHT, fill=tk.Y)

mylist = tk.Listbox(root, yscrollcommand=myscroll.set)
for i in range(50):
    mylist.insert(tk.END, "ITEM " + str(i))
mylist.pack(side=tk.LEFT, fill=tk.BOTH)

myscroll.config(command=mylist.yview)

root.mainloop()


Scale

슬라이더 사용을 위한 Scale 객체이다.

 

RGB같이 미세한 값들을 조절할 때 쓰는 객체이다. Function 하고 연결시켜서 사용할 수 있다. 즉각적인 반응을 확인할 수 있다. 

 

import tkinter as tk

def set_test(var):
    print(var)

root = tk.Tk()
root.geometry('480x320')
root.title('Tkinter Test')
frame = tk.Frame(root)
frame.pack()

scale1 = tk.Scale(frame, from_=0, to=20, command = set_test)
scale1.pack()

scale2 = tk.Scale(frame, from_=0, to=10, length = 200 , tickinterval = 1, command = set_test,
                  orient = tk.HORIZONTAL, sliderlength = 15)
scale2.pack()

root.mainloop()

slider

상세사항은 구글에 Tkinter Documentation 을 검색해보면 많은 자료를 얻을 수 있을 것이다. 

공유하기

facebook twitter kakaoTalk kakaostory naver band