Jump to content


Photo

Chapter 7:get/set Line


  • Please log in to reply
2 replies to this topic

#1 Riluske

Riluske

    New Member

  • Members
  • Pip
  • 1 posts

Posted 31 August 2011 - 07:56 PM

def correct():
    num_good.set(num_good.get()+1)
    sc.play()

I'm pulling this from my code, but if you need an exact reference, it would be on page 253.

Ok, so I'm not so much saying that it doesn't work, I just want to know why, and how were we supposed to get to this without looking at the answer.

I tried working my way through Head First Java, and made it up to the point of encapsulation. When I see get/set, that's what I'm thinking. Is this an example of encapsulation?

Also, I feel as though this is using methods before defining them, for instance, don't we need to define the num_good.set() and num_good.get(), or are we defining them in using the code?

Also (again, I know) why did this have to be done at all? My original thought was to save the methods from before :
def wrong():
    global number_wrong
    global number_asked
    number_asked = number_asked + 1
    number_wrong = number_wrong + 1
    sw.play()

then I realized that I need to get:
number_wrong = IntVar()

into the code. Of course, this is where the whole thing gets messed up. Unless I can add it into the method.

Still, I don't see how we were supposed to get the whole get/set part of the code by ourselves. Can someone explain the thought process that would lead to doing this without actually looking at the answer, or perhaps give me a version of the code that would work that doesn't use this method? Thanks!

#2 esfilho

esfilho

    New Member

  • Members
  • Pip
  • 2 posts

Posted 01 September 2011 - 05:42 PM

Hi Riluske,

I'm posting my code here.
I used only the set() method and used another variable as a parameter. Take a look.

from tkinter import *

import pygame.mixer

sounds = pygame.mixer
sounds.init()

correct_s = sounds.Sound("correct.wav")
wrong_s = sounds.Sound("wrong.wav")

def play_correct_sound():
    global number_correct
    number_correct = number_correct + 1
    num_good.set(number_correct)
    correct_s.play()
    print("Good Job!")
    

def play_wrong_sound():
    global number_wrong
    number_wrong = number_wrong + 1
    num_bad.set(number_wrong)
    wrong_s.play()
    print("Try Again!")

app = Tk()
app.title("TVN Game Show")
app.geometry('300x100+200+100')

b1 = Button(app, text = "Correct!", width = 10, command = play_correct_sound, background = 'green')
b1.pack(side = 'left', padx = 10, pady = 10)

b2 = Button(app, text = "Wrong!", width = 10, command = play_wrong_sound, background = 'red')
b2.pack(side = 'right', padx = 10, pady = 10)

number_correct = 0
num_good = IntVar()
num_good.set(0)

l1 = Label(app, textvariable = num_good)
l1.pack(side = 'left')

number_wrong = 0
num_bad = IntVar()
num_bad.set(0)

l2 = Label(app, textvariable = num_bad)
l2.pack(side = 'right')

app.mainloop()

Hope it helps.

Erick

#3 @$!-!i$!-!

@$!-!i$!-!

    Active Member

  • Members
  • PipPip
  • 13 posts
  • Gender:Male

Posted 07 March 2015 - 01:20 PM

Hey Erick, 

Thanks for your code (for two things):

1. I am not using the get method either. I was setting the IntVar variable somewhere else which didnt work and then I tried your way of setting it and it worked !

2. I liked the background you gave to the buttons !

 

Following is my code:

from tkinter import *
import pygame.mixer

sound = pygame.mixer
sound.init()

app = Tk()
app.title("TVN Game host")
app.geometry('450x100+200+100')
right_answers = IntVar()
wrong_answers = IntVar()
right_answers.set(0)
wrong_answers.set(0)
right_answers_count = 0
wrong_answers_count = 0
correct_s = sound.Sound("correct.wav")
wrong_s = sound.Sound("wrong.wav")

def correct_button_click():
    correct_s.play()
    global right_answers_count
    right_answers_count = right_answers_count + 1
    right_answers.set(right_answers_count)

def wrong_button_click():
    wrong_s.play()
    global wrong_answers_count
    wrong_answers_count = wrong_answers_count + 1
    wrong_answers.set(wrong_answers_count)
    
l3 = Label(app, text='Ready for the quiz?',height=3)
l3.pack()
b1 = Button(app,text="Correct answer",width=15,command=correct_button_click,background='green')
b1.pack(side='left',padx=15,pady=15)
b2 = Button(app,text="Wrong answer",width=15,command=wrong_button_click,background='red')
b2.pack(side='right',padx=15,pady=15)

l1 = Label(app,textvariable = right_answers,height=5)
l1.pack(side = 'left')
l2 = Label(app,textvariable = wrong_answers,height=5)
l2.pack(side = 'right')

app.mainloop()






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users