본문 바로가기
관/핫한 파이썬 공부

파이썬의 여러 모듈 공부하기 03.

by 4차원 박스 2019. 11. 2.

gui수업 3

오늘은 라디오버튼 부터 시작(동그란원 안에 체크 가능한 형태, 즉 설문조사형식)

파이썬의 문자열, 정수 선언 방식
본래는 그냥 변수에 바로 값을 초기화하는 방법이다(앞에 자료형을 붙이지 않고)

하지만 이런방식이 아닌 자료형을 사용하여 선언하는 방식이 있다

다음과 같다.

변수=StringVar( ) 이건 문자열 자료형을 선언함
변수=IntVar( ) 이건 정수 자료형을 선언함


라디오버튼 코드

from tkinter import*
from tkinter import messagebox

root=Tk()

def ch():
    msg="None"
    if num.get()==1:
        msg="java"
    elif num.get()==2:
        msg="python"
    elif num.get()==3:
        msg="c"
    label.configure(text="당신의 choice:"+msg) #버튼누를때 라벨 내용이 이렇게 바뀜
    messagebox.showinfo("your 선택은",msg) #메시지창의 타이틀과 내용
    
title=Label(root, text="programming 언어를 선택하시오")
title.pack()

msg=StringVar() #문자열선언
num=IntVar() #정수 선언

rad1= Radiobutton(text="java", variable=num, value=1)
rad2= Radiobutton(text="python", variable=num, value=2)
rad3= Radiobutton(text="c", variable=num, value=3)

button=Button(root, text="choice", command=ch, width=5, height=1)
label=Label(root, text="당신의 선택은???:") #가장 첫번째 창에서 버튼을 누르기전의 내용임

rad1.pack(side=TOP)
rad2.pack(side=TOP)
rad3.pack(side=TOP)
button.pack()
label.pack()

root.mainloop()

이렇게 작성한다

체크박스는 여러개를 선택할수 있기에
선택할 버튼마다 함수를 선언해주어야 한다.


체크박스 코드


from tkinter import*
def ok1():
    print("python"+str(select1.get()))
    msg=str(select1.get())
    if select1.get()==True:
        label.configure(text="your choice: python selected")
    elif select1.get()==False:
        label.configure(text="your choice: python unselected")
        
def ok2():
    print("java"+str(select2.get()))
    msg=str(select2.get())
    if select2.get()==1:
        label.configure(text="your choice: java selected")
    elif select2.get()==0:
        label.configure(text="your choice: java unselected")
        
win=Tk()
Label(win, text="사용할 언어를 모두 선택").grid(row=0, sticky=W)
label=Label(win,text="your 선택")
label.grid(row=4,column=0)

msg= StringVar()
select1=BooleanVar()
select2=IntVar()

che1= Checkbutton(win, text="python",variable=select1,command=ok1).grid(row=1, sticky=W)
che2= Checkbutton(win, text="java",variable=select2,command=ok2).grid(row=2, sticky=W)

win.mainloop()


리스트박스 코드

from tkinter import*

w=Tk()
s=Scrollbar(w, orient=VERTICAL)
s.pack(side=RIGHT, fill=Y)

l=Listbox(w,height=4)
l.pack()

l.insert(END,'python')
l.insert(END,'c')
l.insert(END,'c++')
l.insert(END,'java')
l.insert(END,'c#')
         
w.mainloop()

스핀박스 코드

from tkinter import*
w=Tk()
w.title('welcome')
w.geometry('350x200')

#spin=Spinbox(w, from_=0, to=100, width=5)
#s=Spinbox(w, values=(3,8,11),width=5)

v=IntVar()
v.set(100)
s=Spinbox(w,from_=0,to=200,width=5,textvariable=v)
s.grid(row=0,column=0)

w.mainloop()

 

 


프로젝트 지금까지해본것들

한번에 출력해보기.
코드는 다음과 같다.







from tkinter import *
from tkinter import messagebox
from tkinter.colorchooser import*
window = Tk()
window.geometry("800x800")
l1=Label(window, text="프로젝트명",font = "Times 35")
l2=Label(window, text="팀명", font = "Times 20")
l1.place(x=250,y=20)
l2.place(x=550,y=70)

def show():
    print("이름: %s\나이: %s" % (e1.get(), e2.get()))
    
l3 = Label(window , text="이름")
l4 = Label(window, text="나이")
l3.place(x=0,y=90)
l4.place(x=0,y=120)
e1 = Entry(window)
e2 = Entry(window)
e1.place(x=30,y=90)
e2.place(x=30,y=120)

b1=Button(window, text="클릭",command=show)
b1.place(x=80,y=140)

def yourChoice():
    msg = "None"
    if number.get() == 1:
        msg = "Java"
    elif number.get() == 2:
        msg = "Python"
    elif number.get() == 3:
        msg = "C"
    label.configure(text="Your choice: " +msg)
    messagebox.showinfo("Your choice: ", msg)
    
title = Label(window, text="Select your language")
title.place(x=20,y=180)

msg = StringVar()
number = IntVar()

r1 = Radiobutton(text="Java", variable=number, value=1)
r2 = Radiobutton(text="Python", variable=number, value=2)
r3 = Radiobutton(text="C", variable=number, value=3)

b2 = Button(window, text="choice", command=yourChoice, width=5, height=1)
label = Label(window, text="Your choice: ")

r1.place(x=30,y=200)
r2.place(x=30,y=220)
r3.place(x=30,y=240)
b2.place(x=30,y=260)
label.place(x=30,y=290)

c = Canvas(window, width=300, height=200)
c.create_text(50, 100, text="팀원의 그림실력",font=("Times",10), fill="black")
color=askcolor()
c.create_rectangle(50, 25, 100, 50 ,fill=color[1])

c.place(x=500,y=150)

spin=Spinbox(window, values=(170,180),width=5)

spin.place(x=300, y=200)

sb = Scrollbar(window, orient=VERTICAL)
sb.place(x=440,y=240)

lb = Listbox(window, height=4)
lb.place(x=300,y=220)

lb.insert(END,"Python")
lb.insert(END,"C")
lb.insert(END,"Java")
lb.insert(END,"C++")
lb.insert(END,"C#")
          

def ok1():
    print("Python"+str(select1.get()))
    msg2 = str(select1.get())
    if select1.get() == True:
        l6.configure(text="Your choice: Python selected") 
    elif select1.get() == False:
        l6.configure(text="Your choice: Python cancelled")
def ok2():
    print("Java"+str(select2.get()))
    msg2 = str(select2.get())
    if select2.get() == True:
        l6.configure(text="Your choice: Java selected") 
    elif select2.get() == False:
        l6.configure(text="Your choice: Java cancelled")  
        
l5=Label(window, text="선호하는 언어를 모두 선택하시오:")
l5.place(x=500,y=480)


select1 = BooleanVar()
check1 = Checkbutton(window, text="Python", variable=select1,command=ok1)
check1.place(x=500,y=510)

select2 = BooleanVar()
check2 = Checkbutton(window, text="Java", variable=select2,command=ok2)
check2.place(x=500,y=530)
l6 = Label(window, text="Your choice")
l6.place(x=500,y=560)

window.mainloop()

댓글