Jump to content


Photo

Arrangement Of Elements On Guis


  • Please log in to reply
5 replies to this topic

#1 Aradalf

Aradalf

    New Member

  • Members
  • Pip
  • 1 posts

Posted 07 May 2011 - 11:49 AM

For the GUI created for TVN in Chapter 7, how would I make the label for number of correct answers and the label for number of wrong answers appear on top of their respective button? This is my code right now:

from tkinter import *
import pygame.mixer
sounds = pygame.mixer

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

sounds.init()

sound_correct = sounds.Sound("correct.wav")
sound_wrong = sounds.Sound("wrong.wav")

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

def button_correct():
    number_correct.set(number_correct.get() + 1)
    sound_correct.play()
    
def button_wrong():
    number_wrong.set(number_wrong.get() + 1)
    sound_wrong.play()

l1 = Label(app, textvariable = number_correct)
l1.pack(side = 'left', padx = 10)

l2 = Label(app, textvariable = number_wrong)
l2.pack(side = 'right', padx = 10)

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

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

app.mainloop()

I would think that should work. Here are my results, along with where I want the numbers to be:

Posted Image

#2 Toni Kukul

Toni Kukul

    Active Member

  • Members
  • PipPip
  • 10 posts
  • Gender:Male

Posted 24 May 2011 - 05:54 PM

Try this code and adapt it to your needs:

from tkinter import *

app = Tk( )
app.title("Grid Geometry Manager")

Label(app, text="Label1", padx=5, pady=5).grid(row=0, column=0)
Label(app, text="Label2", padx=5, pady=5).grid(row=0, column=1)

Button(app, text="Button1", padx=5, pady=5).grid(row=1, column=0)
Button(app, text="Button2", padx=5, pady=5).grid(row=1, column=1)

app.mainloop()


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

@$!-!i$!-!

    Active Member

  • Members
  • PipPip
  • 13 posts
  • Gender:Male

Posted 07 March 2015 - 01:27 PM

Hi mktoni, 

 

I tried your suggestion, but it didnt work. I got the below error:

 

_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack

 

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').grid(row=1,column=0)
b1.pack(side='left',padx=15,pady=15)
b2 = Button(app,text="Wrong answer",width=15,command=wrong_button_click,background='red').grid(row=1,column=1)
b2.pack(side='right',padx=15,pady=15)

l1 = Label(app,textvariable = right_answers,height=5).grid(row=0,column=0)
l1.pack(side = 'left')
l2 = Label(app,textvariable = wrong_answers,height=5).grid(row=0,column=1)
l2.pack(side = 'right')

app.mainloop()



#4 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 15 March 2015 - 04:15 AM

HI @$!-!i$!-! ,

 

Sorry for a late answer.

 

There is a method 'place()' which can be used for that instead of 'pack' or 'grid'.

 

See the below code.

from tkinter import *
import pygame.mixer

sound = pygame.mixer
sound.init()

app = Tk()
app.title("TVN Game host")
app.geometry('500x300+400+300')

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", font = 'Arial -20 bold', width = 15, height = 5,command=correct_button_click,background='green')
b1.pack(side='left',padx=15,pady=15)
b2 = Button(app,text="Wrong answer", font = 'Arial -20 bold', width = 15, height = 5,command=wrong_button_click,background='red')
b2.pack(side='right',padx=15,pady=15)

l1 = Label(app,textvariable = right_answers, font = 'Arial -20 bold', background='green').place(x = -150, y = 70, relx=0.5, rely=0.5, anchor=W) #The x, y can be easily found out after few trials as relx and rely is at center, i.e., 0.5
#l1.pack(side = 'left')
l2 = Label(app,textvariable = wrong_answers, font = 'Arial -20 bold', background='red').place(x = 150, y = 70, relx=0.5, rely=0.5, anchor=E)
#l2.pack(side = 'right')

app.mainloop()

I have also changed some fonts and backgrounds of buttons and labels in your code. You can tweak other's similarly. Enjoy !!!

cool.png smile.png


Edited by #TM#, 15 March 2015 - 04:19 AM.


#5 Toni Kukul

Toni Kukul

    Active Member

  • Members
  • PipPip
  • 10 posts
  • Gender:Male

Posted 27 March 2015 - 08:14 AM

@$!-!i$!-!:

Use grid or pack geometry manager, not both. You cannot use geometry manager grid inside an application that already has slaves managed by pack.

 

Try this code, it should works:

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.grid(row=0,column=0, columnspan=2)
l1 = Label(app,textvariable = right_answers,height=5).grid(row=1,column=0)
##l1.pack(side = 'left')
l2 = Label(app,textvariable = wrong_answers,height=5).grid(row=1,column=1)
##l2.pack(side = 'right')
b1 = Button(app,text="Correct answer",width=15,command=correct_button_click,background='green').grid(row=2,column=0)
##b1.pack(side='left',padx=15,pady=15)
b2 = Button(app,text="Wrong answer",width=15,command=wrong_button_click,background='red').grid(row=2,column=1)
##b2.pack(side='right',padx=15,pady=15)

app.mainloop()

Sorry for late reply.



#6 @$!-!i$!-!

@$!-!i$!-!

    Active Member

  • Members
  • PipPip
  • 13 posts
  • Gender:Male

Posted 04 April 2015 - 04:28 PM

Thank You #TM# and Toni !






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users