클래스를 올바르게 사용하고 있는지 잘 모르겠습니다. 파이 게임을 사용하여 간단한 메뉴를 만들려고합니다. 이것은 gui 물건에 대한 나의 첫 번째 진출이다. 내 코드를 구조화하는 방법을 모른다.클래스 "A"에서 클래스 "B"의 인스턴스 메서드를 어떻게 호출합니까?
모든 마우스 오버/마우스 클릭 물건을 처리 할 수있는 일반 Button 클래스를 만든 다음 각 버튼에 대해 하위 클래스로 만들고 do_action
메서드를 재정 의하여 각 버튼에 특정 작업을 부여 할 수 있습니다. 당신이 볼 수있는
class Button(pygame.sprite.Sprite):
global screen_flags
def __init__(self, images, pos):
pygame.sprite.Sprite.__init__(self)
self.images = images
self.image = images[0]
self.rect = self.image.get_rect()
self.rect.move_ip(pos)
def update(self, events, surface):
# if not screen_flags['config']:
for event in events:
if event.type == MOUSEMOTION:
if self.rect.collidepoint(event.pos):
self.image = self.images[1]
else:
self.image = self.images[0]
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1 and self.rect.collidepoint(event.pos):
self.image = self.images[-1]
screen_flags['config'] = 1
self.do_action()
self.set_flag()
elif event.type == MOUSEBUTTONUP:
self.image = self.images[0]
screen.blit(self.image, self.rect)
def do_action(self):
pass
def set_flag(self):
pass
class CheckBox(Button):
def __init__(self, images, pos):
Button.__init__(self, images, pos)
self.is_clicked = False
def update(self, events, surface):
for event in events:
if event.type == MOUSEMOTION:
if not self.is_clicked:
if self.rect.collidepoint(event.pos):
self.image = self.images[1]
else:
self.image = self.images[0]
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1 and self.rect.collidepoint(event.pos):
if not self.is_clicked:
self.image = self.images[-1]
self.is_clicked = True
else:
self.image = self.images[0]
self.is_clicked = False
screen.blit(self.image, self.rect)
class Cancel(Button):
def do_action(self):
screen_flags['config'] = 0
그래서, 그들은 정말 아직 아무것도하지 않습니다. 나는 체크 박스를 켜고 끌 수 있으며, 하나의 'config'창을 열고 닫을 수있다. 그러나 그것은 내가 얻은 것까지이다. 나머지 코드는 다음과 같습니다.
global count
global sTime
config_button_img = load_sliced_images(25, 25, c_buttons)
config_button = Button(config_button_img, (608,4))
input_bar = load_sliced_images(351,33, inpt_bar)
text_box = Textbox(input_bar, (144,155))
s_button = load_sliced_images(110,32, sbmt_bttn)
submit = Button(s_button, (241,301))
c_button = load_sliced_images(110,32, cncl_bttn)
cancel = Cancel(c_button, (385, 301))
c_boxes = load_sliced_images(20,19, chk_box)
check_box = CheckBox(c_boxes, (200,200))
try:
while True:
# ****************************
# Pygame section
events = pygame.event.get()
for event in events:
if event.type == QUIT:
screen_flags['alert'] = 1
ding.play(0,1000)
elif event.type == MOUSEBUTTONDOWN:
text_box.set_focus(event.button, event.pos)
screen.blit(background, (0,0))
screen.blit(client_logo, (0,30))
screen.blit(tag, (174,462))
if screen_flags['config']:
screen.blit(config_window_img, (0,0))
submit.update(events, screen)
cancel.update(events, screen)
check_box.update(events, screen)
if screen_flags['alert']:
screen.blit(alert_dialog, (0,0))
config_button.update(events, screen)
pygame.display.update()
except KeyboardInterrupt:
try:
pygame.quit()
except:
pass
이렇게 예상대로 작동합니다. 나는 여기서 어디로 가야할지 모르겠다. 수업 내에서 논리를 계속 정리합니까? 예를 들어, 내가하려고하는 다음 작업은 "취소"버튼을 클릭 할 때 확인란을 선택 취소하도록하는 것입니다.
그냥 Cancel
클래스를 변경해 보았습니다.
class Cancel(Button):
def do_action(self):
screen_flags['config'] = 0
check_box.is_clicked=False
그러나이 때문에 GlobalName 오류가 발생합니다. 다른 클래스에서 인스턴스 메서드를 어떻게 대상으로합니까? 이것도 올바른 방법일까요? 또는 update()
과 같은 논리를 가지고 마우스를 돌봐야한다면 은과 같은 변수를 다른 클래스와주고받습니다. main()
? 모든 클래스에서 전역 변수를 사용해야합니까?
gui 사례에 대한 유용한 자료가 있습니까? 코드 등을 구조화하는 것과 같은 것들.
위의 내용이 의미가 있습니다.
클래스'A' 객체에 대한 참조를 얻고 메소드를 호출 하시겠습니까? –