본문 바로가기
Python, C, C++

Example of Python using tkinter module.

by 소나무기운 2022. 2. 24.
반응형

[2022/04/06] Add title Image

[2022/02/24] The Beginning

 

소나무 기운 ,  전자제품 개발/생산

Example of Python using tkinter module.

Let's find out how to make a menu using tkinter module.

tkinter is the basic module included inpython.

 

 

 

Simple menu configuration example

from tkinter import *
from tkinter import filedialog

def Loadfile():
    filename = filedialog.askopenfilename(initialdir="/", title="Select file",
                                          filetypes=(("Image files", "*.png"), ("all files", "*.*")))
    print(filename)

def AboutProgram():
    print("tkinter Menu Example")

root = Tk()
root.title('tkinter Menu')
menubar = Menu(root)
menufile = Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=menufile)
menufile.add_command(label="Open", command=Loadfile)
menufile.add_separator()
menufile.add_command(label="Exit", command=root.quit)
menuhelp = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Help", menu=menuhelp)
menuhelp.add_command(label="About", command=AboutProgram)

root.config(menu=menubar)
root.mainloop()

Decorate the menu with appropriate separation lines.

 

 

Change the size of Form (Can't see the window title)

root.geometry("500x200")

 

 

 

마무리

The menu can be simply organized.

 

Next time, let's add an icon in front fo the menu or put a shortcut key.

 

 

from tkinter import *
from tkinter import filedialog

def Loadfile():
    filename = filedialog.askopenfilename(initialdir="/", title="Select file",
                                          filetypes=(("Image files", "*.png"), ("all files", "*.*")))
    print(filename)

def AboutProgram():
    print("tkinter Menu Example")

root = Tk()
root.title('tkinter Menu')
root.geometry("500x200")
menubar = Menu(root)
menufile = Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=menufile)
menufile.add_command(label="Open", command=Loadfile)
menufile.add_separator()
menufile.add_command(label="Exit", command=root.quit)
menuhelp = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Help", menu=menuhelp)
menuhelp.add_command(label="About", command=AboutProgram)

root.config(menu=menubar)
root.mainloop()

menu.py
0.00MB

 

 

참고문헌

 

 

 

 

 

틀린 부분이나 질문은 댓글 달아주세요.

즐거운 하루 보내세요. 감사합니다.

 

 

반응형

댓글