본문 바로가기

개발자 모드/파이썬(python)

파이썬 GUI tkinter 메뉴바 생성 함수 실행

728x90
#=======================================
# imports
#=======================================

import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
from tkinter import Menu

# Create instance

win = tk.Tk()

# Add a title
win.title("Python GUI")


# Adding a Label
a_label = ttk.Label(win, text="A label")
a_label.grid(column =0, row =0)



# Button Click Event Function
def click_me():
    action.configure(text ="hello "+ name.get()+ "  " + number_chosen.get())

# Change our Label
ttk.Label(win, text ="Enter a name :").grid(column =0, row =0)



# Adding a text box Entry widget
name = tk.StringVar()
name_entered = ttk.Entry(win, width =12, textvariable= name)
name_entered.grid(column =0, row =1)

# Adding a button
action = ttk.Button(win, text = "Click me!", command = click_me)
action.grid(column =2, row =1)

ttk.Label(win, text ="choose a number: ").grid(column =1, row=0)
number = tk.StringVar()
number_chosen = ttk.Combobox(win, width =12, textvariable = number, state ="readonly")
number_chosen['value'] =(1,2,4,42,100)
number_chosen.grid(column =1, row =1)
number_chosen.current(0)
# Creating three checkbuttons
chVarDis = tk.IntVar()
check1 = tk.Checkbutton(win, text ="Disalbed", variable = chVarDis, state ="disabled")
check1.select()
check1.grid(column =0, row =4, sticky =tk.W)

chVarUn = tk.IntVar()
check2 = tk.Checkbutton(win, text ="Unchecked", variable = chVarUn)
check2.deselect()
check2.grid(column =1, row =4, sticky =tk.W)

chVarEn = tk.IntVar()
check3 = tk.Checkbutton(win, text ="Enalbed", variable = chVarEn)
check3.select()
check3.grid(column =2, row =4, sticky =tk.W)














# Radiobutton Globals
colors = ["Blue", "Gold", "Red"]

# We have also changed the callback function to be zero-based, using the list
# instead of module- level golbal variables
# Radiobutton Callbaack
def radCall() :
    radSel = radVar.get()
    if radSel   == 0  : win.configure(background = colors[0]) 
    elif radSel == 1  : win.configure(background = colors[1])   
    elif radSel == 2  : win.configure(background = colors[2]) 
    
# create three Radiobuttons using one variable
radVar = tk.IntVar()

# Next we are selecting aa non-existing index value for radVar
radVar.set(99)

# Now we are creating all three Radiobutton widgets within one loop
for col in range(3) :
    curRad = tk.Radiobutton(win, text =colors[col], variable=radVar,
                            value = col, command =radCall)
    curRad.grid(column=col, row =5, sticky =tk.W)

# using aa scrolled text control
scrol_w = 30
scrol_h = 3
scr = scrolledtext.ScrolledText(win, width = scrol_w, height = scrol_h, wrap = tk.WORD)
scr.grid(column =0, columnspan=3)

name_entered.focus()

# create a container to hold labes
buttons_frame = ttk.LabelFrame(win, text =" Labels in a Frame")
buttons_frame.grid(column =0, row =7) 
# buttons_frame.grid(column =1, row =7) 

# Place labels into the container element
ttk.Label(buttons_frame, text ="Label1").grid(column =0, row =0, sticky = tk.W)
ttk.Label(buttons_frame, text ="Label2").grid(column =0, row =1, sticky = tk.W)
ttk.Label(buttons_frame, text ="Label3").grid(column =0, row =2, sticky = tk.W)


# Exit GUI cleanly
def _quit() :
    win.quit()
    win.destroy()
    exit



# Creating a Menu Bar
menu_bar = Menu(win)
win.config(menu=menu_bar)

# Create menu and add menu items
file_menu = Menu(menu_bar, tearoff =0)
file_menu.add_command(label = "New")
file_menu.add_separator()
file_menu.add_command(label ="Exit", command = _quit)
menu_bar.add_cascade(label="File", menu=file_menu)

# Add another Menu to the Menu Bar and an item

help_menu = Menu(menu_bar, tearoff =0)
menu_bar.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label ="About")


#=======================================
# Start GUI
#=======================================
win.mainloop()

 

728x90