티스토리 뷰

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

아래 예제에서는 틀의 크기,위치를 지정하고 틀의 테두리모양을 주는방법이다.

우선 예제를 본후 설명하도록 하겠다.

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

108

# -*- coding : cp949 -*-

from tkinter import *

 

class MyApp:

    def __init__(self, parent):

        self.myParent=parent

       

        # ----- myContainer1 -----

        self.myContainer1=Frame(parent)

        self.myContainer1.pack()

       

        # ----- 조감을 제어하는에 필요한 상수들 -----

        button_width=6

        button_padx="2m"

        button_pady="1m"

        buttons_frame_padx="3m"

        buttons_frame_pady="2m"

        buttons_frame_ipadx="3m"

        buttons_frame_ipady="1m"

        # ----- 상수 끝 -----

       

        ### myContainer1안에 수직적 동선을 사용한다.

        ### myContainer1안에 먼저 buttons_frame을 만든 후

        ### top_frame bottom_frame를 만든다

       

        # ----- buttons_frame -----

        self.buttons_frame=Frame(self.myContainer1,background="blue")

        self.buttons_frame.pack(side=TOP,

                                ipadx=buttons_frame_ipadx,

                                ipady=buttons_frame_ipady,

                                padx=buttons_frame_padx,

                                pady=buttons_frame_pady)

       

        # ----- top_frame -----

        self.top_frame=Frame(self.myContainer1)

        self.top_frame.pack(side=TOP,

                            fill=BOTH,

                            expand=YES)

       

        # ----- bottom_frame -----

        self.bottom_frame=Frame(self.myContainer1,

                                borderwidth=5, #틀 테두리 두꼐

                                relief=RIDGE, #틀 모양

                                height=50, #틀 높이

                                background="white")

        self.bottom_frame.pack(side=TOP,

                               fill=BOTH, #빈공간 채우기

                               expand=YES)

       

        ### left_frame right_frame이라는

        ### 두개의 틀을 top_frame안에 배치한다.

        ### top_frame안에 수평적동선을 사용한다.

       

        # ----- left_frame -----

        self.left_frame=Frame(self.top_frame,

                              background="red",

                              borderwidth=5,

                              relief=RIDGE,

                              height=250,

                              width=50)

        self.left_frame.pack(side=LEFT,

                             fill=BOTH,

                             expand=YES)

       

        # ----- right_frame -----

        self.right_frame=Frame(self.top_frame,

                               background="tan",

                               borderwidth=5,

                               relief=RIDGE,

                               width=250)

        self.right_frame.pack(side=RIGHT,

                              fill=BOTH,

                              expand=YES)

       

        ### 버튼들을 buttons_frame에 추가한다.

       

        # ----- button1 -----

        self.button1=Button(self.buttons_frame, command=self.button1Click)

        self.button1.bind("<Return>"self.button1Click)

        self.button1.configure(text="OK", background="green",

                               width=button_width,

                               padx=button_padx,

                               pady=button_pady)

        self.button1.focus_force() #초기 초점 지정

        self.button1.pack(side=LEFT)

       

        # ----- button2 -----

        self.button2=Button(self.buttons_frame, command=self.button2Click)

        self.button2.bind("<Return>"self.button2Click)

        self.button2.configure(text="Cancel", background="red",

                               width=button_width,

                               padx=button_padx,

                               pady=button_pady)

        self.button2.pack(side=RIGHT)

       

    # ----- Functions -----

    def button1Click(self,event=None):

        if self.button1["background"]=="green":

            self.button1.configure(background="yellow")

        else:

            self.button1.configure(background="green")

   

    def button2Click(self,event=None):

        self.myParent.destroy() #어플리케이션 닫기

       

root=Tk()

myapp=MyApp(root)

root.mainloop()

실행



위 어플리케이션은 총 6개의 프레임을 갖는다.

기본적으로 모든 부품이 들어가는 myContainer1 틀을 먼저생성후

myContainer1 안에 buttons_frametop_frame, bottom_frame이 생성된다.

buttons_frame의 배경색은 파랑색이며버튼창부품 2개가 들어간다.

top_frame안에는 left_frame right_frame이 들어가며각각 빨간색과 갈색의 배경색을 갖는다.

bottom_frame의 배경색은 하얀색을 갖는다.

위 예제에서 보여주는것은,

틀안에 틀이 존재할수 있으며각 틀의 크기는 신축성이 있어서 틀안에부품이 들어올경우 해당 부품에 맞게 틀의 크기가 조절된다.

틀 안에 아무내용이 없다면틀을 생성하여도 화면상으로 보여지지 않는데이때 height width를이용하여 사이즈를 지정하여 틀안에 내용이 없어서 보여질수 있도록 하였다.

 

borderwidth를 이용하면 틀의 테두리 두께를 지정할수 있고,

relief를 이용하면 틀의 테두리모양을 지정할수 있다.

 

아래는 프레임의 테두리모양,배경색,테두리두께를 지정하여 확인할수 있도록 만든 예제이다. 

 


 

> 코드보기

# -*- coding:utf-8 -*-
from tkinter import *
from tkinter import colorchooser

class MyApp:
    def __init__(self,parent):
        
        button_width=10
        
        self.myparent = parent
        parent.geometry("300x500")
        
        frame_reliefs = [FLAT, GROOVE, RAISED, RIDGE, SOLID, SUNKEN]
        
        self.frame_relief = StringVar()
        self.frame_relief.set(FLAT) #기본값
        self.frame_bg = StringVar()
        self.frame_bg.set("SystemButtonFace") #기본값
        self.frame_borderwidth=IntVar()
        self.frame_borderwidth.set(0) #기본값
        
        self.top_F = Frame(self.myparent)
        self.top_F.pack(side=TOP,expand=YES,fill=BOTH,padx=3,pady=3)
        self.top_test_F = Frame(self.top_F)
        self.top_test_F.pack(expand=YES,fill=BOTH,padx=10,pady=10)
        
        self.bottom_F = Frame(self.myparent)
        self.bottom_F.pack(side=BOTTOM,padx=3,pady=3)
        
        self.bottom_relief_F = Frame(self.bottom_F)
        self.bottom_relief_F.pack(side=LEFT,padx=3,fill=Y)
        Label(self.bottom_relief_F,text="frame\nrelief").pack(padx=3,pady=3)
        for item in frame_reliefs:
            Radiobutton(self.bottom_relief_F,
                        text=item,
                        value=item,
                        variable=self.frame_relief,
                        command=self.update,
                        indicator=0,
                        width=button_width,
                       ).pack()
        
        self.bottom_bg_F = Frame(self.bottom_F)
        self.bottom_bg_F.pack(side=LEFT,padx=3,fill=Y)
        Label(self.bottom_bg_F,text="change\nbackground").pack(padx=3,pady=3)
        Button(self.bottom_bg_F,
               text="set default",
               command=lambda t=True:self.bg_get(t),
               width=button_width,
               ).pack()
        Button(self.bottom_bg_F,
               text="Change",
               command=self.bg_get,
               width=button_width,
              ).pack()
        self.update()
        
        self.bottom_borderwidth_F = Frame(self.bottom_F)
        self.bottom_borderwidth_F.pack(side=LEFT,padx=3,fill=Y)
        Label(self.bottom_borderwidth_F,text="change\nborderwidth").pack(padx=3,pady=3)
        self.bottom_berderwidth_Entry=Entry(self.bottom_borderwidth_F,width=button_width)
        self.bottom_berderwidth_Entry.pack()
        Button(self.bottom_borderwidth_F,
               text="Accept",
               command=self.borderwidth_get,
               width=button_width,
              ).pack()
        Label(self.bottom_borderwidth_F,text="default : 0").pack()
        
    def update(self):
        self.top_test_F.configure(relief=self.frame_relief.get(),
                                  bg=self.frame_bg.get(),
                                  borderwidth=self.frame_borderwidth.get(),
                                 )
        print(self.frame_relief.get())
        print(self.frame_bg.get())
        print(self.frame_borderwidth.get())
    
    def bg_get(self,default=False):
        if default:
            self.frame_bg.set("SystemButtonFace")
        else:
            color=colorchooser.askcolor()[1]
            if color:
                self.frame_bg.set(color)
        self.update()
    
    def borderwidth_get(self):
        self.frame_borderwidth.set(self.bottom_berderwidth_Entry.get())
        self.update()

root=Tk()
myapp=MyApp(root)
root.mainloop()

 

 

 

 

참조 : http://coreapython.hosting.paran.com/GUI/Thinking%20in%20Tkinter.htm

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함