파이썬(python) - 버튼 이벤트 코드 연결하기
버튼을 누를 때 tkinter 이벤트 루프는 이 이벤트를 포착하고 이 이벤트에 연결된 것이 있는지 찾아 봅니다.
버튼에 코드를 연결하기 위해서는 실행할 코드를 하나의 함수로 만들고,
버튼을 만들 때 command인자로 실행할 함수의 이름을 지저합니다.
def button_click():
print("I've Just veen clicked!") // 이벤트가 발생할 때 실행할 코드를 갖고 있는 함수를 생성합니다.
b = button(app, text ="click !", width=15, command = button_click) //버튼이 눌릴대 싱핼할 함수의 이름을 지정합니다.
print("I've Just veen clicked!") // 이벤트가 발생할 때 실행할 코드를 갖고 있는 함수를 생성합니다.
b = button(app, text ="click !", width=15, command = button_click) //버튼이 눌릴대 싱핼할 함수의 이름을 지정합니다.
버튼 이벤트 코드
from tkinter import*
import pygame.mixer
sounds = pygame.mixer
sounds.init()
sounds.init()
correct_s = sounds.sound("correct.wav")
wrong_s = sounds.sound("wrong.wav")
wrong_s = sounds.sound("wrong.wav")
number_correct = 0
number_wrong = 0
number_wrong = 0
def play_correct_sound():
global number_correct
number_correct = number_correct + 1
correct_s.play()
global number_correct
number_correct = number_correct + 1
correct_s.play()
def play_worng_sound():
global number_wrong
number_wrong = number_wrong + 1
wrong_s.play()
global number_wrong
number_wrong = number_wrong + 1
wrong_s.play()
app = tk()
app.title("your thinter application")
app.geometry('450x100+200+100')
app.title("your thinter application")
app.geometry('450x100+200+100')
b1 = Button(app, text = "correct" , width = 10, \
command = play_correct_sound)
b1.pack(side='left', padx =10, pady=10)
command = play_correct_sound)
b1.pack(side='left', padx =10, pady=10)
b2 = Button(app, text = "Wrong!" , width = 10, \
command = play_worng_sound)
b2.pack(side='right', padx =10, pady=10)
command = play_worng_sound)
b2.pack(side='right', padx =10, pady=10)
app.minloop()
print(str(number_correct) + "were correctly ansqwewd.")
print(str(number_wrong) + "were ansqwewd incorrectly.")
print(str(number_wrong) + "were ansqwewd incorrectly.")
'' 카테고리의 다른 글
파이썬(Python) - GUI 데이터 입력 위젯 사용하기 (0) | 2011.11.07 |
---|---|
파이썬(Python) - GUI 인터페이스 요소 위젯 , 레이블 위젯 (0) | 2011.11.03 |
파이썬(python) - 버튼 이벤트 코드 연결하기 (0) | 2011.11.01 |
파이썬(Python) - 그래픽 사용자 인터페이스(GUI) 구현. 버튼 만들기 (0) | 2011.11.01 |
파이썬(python) - 모듈화 (0) | 2011.10.31 |
파이썬(python) - 스트링을 사용하여 스트링을 포멧 (0) | 2011.10.29 |
댓글을 달아 주세요