[참고]

import tkinter as tk

# Create instance
win = tk.Tk()

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

# Win Size Control
win.geometry('500x200')
win.resizable(width=1, height=0)

# Start GUI
win.mainloop()

[결과]

[정리]

# Win Size Control
(1) win.geometry('500x200')

  • 이 코드에서 '500x200'(문자열)은 창 사이즈를 말함. 원하는 사이즈를 입력해서 변경하면 됨
  • 여기서 x는 곱하기가 아니라 알파벳 소문자 엑스임

(2) win.resizable(width=1, height=0)

  • 이 코드에서 width(너비=가로), height(높이=세로)는 부울(True or False) 값을 넣어주면 됨
  • win.resizable(width=1, height=0) #가로만 조절가능하게 하고 싶을 때
  • win.resizable(width=0, height=1) #세로만 조절가능하게 하고 싶을 때
  • win.resizable(width=1, height=1) #가로,세로 둘 다 조절가능하게 하고 싶을 때
  • win.resizable(width=0, height=0) #가로,세로 둘 다 조절 불가능하게 하고 싶을 때

 

 

[참고]

 

1. 소스

# -*- coding: utf-8 -*-

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

win = tk.Tk()

win.title("Python GUI")

#Label 만들기
a_label = ttk.Label(win, text = "A Label")
a_label.grid(column = 0, row = 0)

#버튼 동작함수
def click_me():
    action.configure(text = 'Hello ' + name.get() + ' '+ number_chosen.get())

#Label 만들기
ttk.Label(win, text = "Enter a name:").grid(column = 0, row = 0)

# 빈 텍스트박스 위젯 만들기
name = tk.StringVar()
name_entered = ttk.Entry(win, width = 12, textvariable = name)
name_entered.grid(column = 0, row = 1)

#버튼 만들기
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['values'] = (1,2,4,42,100)
number_chosen.grid(column = 1, row = 1)
number_chosen.current(0)

#체크버튼 만들기
chVarDis = tk.IntVar()
check1 = tk.Checkbutton(win, text='Disabled', 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)

chVarEn = tk.IntVar()
check3 = tk.Checkbutton(win, text= 'Enabled', variable = chVarEn)
check3.deselect()
check3.grid(column = 2, row = 4, sticky = tk.W)

#GUI Callback 함수
def checkCallback(*ignoredArgs):

    if chVarUn.get():
        check3.configure(state = 'disabled')
    else:
        check3.configure(state = 'normal')
    if chVarEn.get():
        check2.configure(state = 'disabled')
    else:
        check2.configure(state = 'normal')

#2개 체크버튼 상태 추적
chVarUn.trace('w', lambda unused0, unused1, unused2 : checkCallback())
chVarEn.trace('w', lambda unused0, unused1, unused2 : checkCallback())

#스크롤 문자 사용
scrol_w = 30
scrol_h = 3
scr = scrolledtext.ScrolledText(win, width = scrol_w, height = scrol_h, wrap = tk.WORD)
scr.grid(column = 0, row = 5, columnspan = 3)

#라디오 버튼을 리스트로 변경
colors = ["Blue", "Gold", "Red"]

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])

# 한 개의 변수를 사용해 3개의 라디오 버튼 생성
radVar = tk.IntVar()

#radVar에 존재하지 않는 인덱스 값 선택
radVar.set(99)

#한 루프 이내에 3개의 라디오 버튼 생성
for col in range(3):
    curRad = tk.Radiobutton(win, text = colors[col], variable = radVar, value = col, command = radCall)
    curRad.grid(column = col, row = 6, sticky = tk.W)

#라벨을 유지하기 위한 컨테이너 생성
buttons_frame = ttk.LabelFrame(win, text='Labels in a Frame ')
buttons_frame.grid(column = 1, row = 7)

#컨테이너 요소로 라벨 놓기
ttk.Label(buttons_frame, text = 'Label1').grid(column = 0, row = 0, sticky = tk.W)
ttk.Label(buttons_frame, text = 'Label2').grid(column = 1, row = 0, sticky = tk.W)
ttk.Label(buttons_frame, text = 'Label3').grid(column = 2, row = 0, sticky = tk.W)

#name Entry로 커서이동
name_entered.focus()

#GUI 시작
win.mainloop()

2. 결과

 

[참고]


import tkinter as tk

from tkinter import ttk


#create instance

win = tk.Tk()


#add a title

win.title("Python GUI")


tabControl = ttk.Notebook(win)  #create tab control


tab1 = ttk.Frame(tabControl)             #create a tab

tabControl.add(tab1, text = 'Tab 1')     #add the tab

tab2 = ttk.Frame(tabControl)             #add a second tab

tabControl.add(tab2, text = 'Tab 2')     #make second tab visible


tabControl.pack(expand = 1, fill = "both") #pack to make visible


#LabelFrame using tab1 as the parent

mighty = ttk.LabelFrame(tab1, text = 'Mighty Python')

mighty.grid(column = 0, row = 0, padx = 8, pady = 4)


#Label using mighty as the parent

a_label = ttk.Label(mighty, text = "Enter a name:")

a_label.grid(column = 0, row = 0, stick = 'W')


#start GUI

win.mainloop()



[결과]



[참고]

import tkinter as tk

from tkinter import ttk


#create instance

win = tk.Tk()


#add a title

win.title("Python GUI")


#create tab control

tabControl = ttk.Notebook(win)


tab1 = ttk.Frame(tabControl)             #create a tab

tabControl.add(tab1, text = 'Tab 1')     #add the tab

tab2 = ttk.Frame(tabControl)             #add a second tab

tabControl.add(tab2, text = 'Tab 2')     #add second tab


tabControl.pack(expand = 1, fill = "both") #pack to make visible


#start GUI

win.mainloop()


[결과]



[참고]

import tkinter as tk
from tkinter import ttk

win = tk.Tk()                                    # Create instance
win.title("Python GUI")                       # Add a title       
tabControl = ttk.Notebook(win)           # Create Tab Control
tab1 = ttk.Frame(tabControl)               # Create a tab 
tabControl.add(tab1, text='Tab 1')        # Add the tab
tabControl.pack(expand=1, fill="both")  # Pack to make visible

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


[결과]


[참고]


#GUI_radiobutton_widget.py

import tkinter as tk
from tkinter import ttk

#create instance
win = tk.Tk()

#add a title
win.title("Pyton GUI")

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

#Modified Button Click Function
def click_me():
    action.configure(text = 'Hello ' + name.get() + ' ' + number_chosen.get())

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

#Adding a Textbox 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)

#Creating three checkbuttons
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['values'] = (1,2,4,42,100)
number_chosen.grid(column = 1, row = 1)
number_chosen.current(0)

chVarDis = tk.IntVar()
check1 = tk.Checkbutton(win, text = "Disabled", 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 = "Enabled", variable = chVarEn)
check3.deselect()
check3.grid(column = 2, row = 4, sticky = tk.W)

#GUI Callback function
def checkCallback(*ignoredArgs):
    #only enable one checkbutton
    if chVarUn.get() :
        check3.configure(state = 'disabled')
    else:
        check3.configure(state = 'normal')
    if chVarEn.get():
        check2.configure(state = 'disabled')
    else:
        check2.configure(state = 'normal')

#trace the state of the two checkbuttons
chVarUn.trace('w', lambda unused0, unused1, unused2 : checkCallback())
chVarEn.trace('w', lambda unused0, unused1, unused2 : checkCallback())

#Radiobutton Globals
COLOR1 = "Blue"
COLOR2 = "Gold"
COLOR3 = "Red"
COLOR4 = "Black"

#Radiobutton Callback
def radCall():
    radSel = radVar.get()
    if radSel == 1:
        win.configure(background = COLOR1)
    elif radSel == 2:
        win.configure(background = COLOR2)
    elif radSel == 3:
        win.configure(background = COLOR3)
    elif radSel == 4:
        win.configure(background = COLOR4)

#create three Radiobuttons using one variable
radVar = tk.IntVar()

rad1 = tk.Radiobutton(win, text = COLOR1, variable = radVar, value = 1, command = radCall)
rad1.grid(column = 0, row = 5, sticky = tk.W, columnspan = 3)

rad2 = tk.Radiobutton(win, text = COLOR2, variable = radVar, value = 2, command= radCall)
rad2.grid(column = 1, row = 5, sticky = tk.W, columnspan = 3)

rad3 = tk.Radiobutton(win, text = COLOR3, variable = radVar, value = 3, command = radCall)
rad3.grid(column = 2, row = 5, sticky = tk.W, columnspan = 3)

rad4 = tk.Radiobutton(win, text = COLOR4, variable = radVar, value = 4, command = radCall)
rad4.grid(column = 3, row = 5, sticky = tk.W, columnspan = 3)

name_entered.focus()

#start GUI
win.mainloop()

[결과]


'#Python GUI Programming Cookbook' 카테고리의 다른 글

#Tab 안에 LabelFrame과 Label 넣기  (0) 2018.12.21
#Tab 2개 만들기  (0) 2018.12.21
#Tab 만들기  (0) 2018.12.21
#콤보박스 위젯 만들기  (0) 2018.12.07
#tkinter #메시지 박스 만들기  (0) 2018.12.06

[참고]


#======================

# imports

#======================

import tkinter as tk

from tkinter import ttk


# Create instance

win = tk.Tk()   


# Add a title       

win.title("Python GUI")  


# Modify adding a Label

a_label = ttk.Label(win, text="A Label")

a_label.grid(column=0, row=0)


# Modified Button Click Function

def click_me(): 

    action.configure(text='Hello ' + name.get())

#     print(number)

#     print(number.get())


# Changing our Label

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


# Adding a Textbox Entry widget

name = tk.StringVar()

name_entered = ttk.Entry(win, width=12, textvariable=name)

name_entered.grid(column=0, row=1)                          # column 0


# Adding a Button

action = ttk.Button(win, text="Click Me!", command=click_me)   

action.grid(column=2, row=1)                                # <= change column to 2


ttk.Label(win, text="Choose a number:").grid(column=1, row=0)

number = tk.StringVar()

number_chosen = ttk.Combobox(win, width=12, textvariable=number)

number_chosen['values'] = (1, 2, 4, 42, 100)

number_chosen.grid(column=1, row=1)                         # <= Combobox in column 1

number_chosen.current(0)


name_entered.focus()      # Place cursor into name Entry

#======================

# Start GUI

#======================

win.mainloop()



[결과]

(가운데 숫자1 있는 박스가 콤보 박스)



[참고]


#GUI__init.py

#======================

# imports

#======================

import tkinter as tk

from tkinter import ttk


#======================

# Create instance

#======================

win = tk.Tk()   


#======================

# Add a title       

#====================== 

win.title("Python GUI")


#===============================================================================

# Adding a LabelFrame and a Button

#===============================================================================

lFrame = ttk.LabelFrame(win, text="Python GUI Programming Cookbook")

lFrame.grid(column=0, row=0, sticky='WE', padx=10, pady=10)


def clickMe():

    from tkinter import messagebox

    messagebox.showinfo('Message Box', 'Hi from same Level.')

     

button = ttk.Button(lFrame, text="Click Me ", command=clickMe)

button.grid(column=1, row=0, sticky=tk.S)  


#======================

# Start GUI

#======================

win.mainloop()



[결과]



'#Python GUI Programming Cookbook' 카테고리의 다른 글

#Tab 안에 LabelFrame과 Label 넣기  (0) 2018.12.21
#Tab 2개 만들기  (0) 2018.12.21
#Tab 만들기  (0) 2018.12.21
#tkinter #체크 버튼 #라디오 버튼 만들기  (0) 2018.12.17
#콤보박스 위젯 만들기  (0) 2018.12.07

+ Recent posts