Jump to content

BEGGINER problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
icystef

icystef

    Newbie

  • Members
  • Pip
  • 4 posts
Hi i'm a begginer i'm making a program and i im encountering a little problem first here is my code :

#othello projet prog

from tkinter import *
from random import randrange

#interface graphique

def couleurAlea():  
    global couleur
    alea =['yellow','cyan','blue','green','grey','purple','orange','pink','dark green','red']
    c = randrange(10)
    couleur = alea[c]
    can1.configure(bg=couleur)

def creerTable():
    global x1, y1, x2, y2
    v=0
    h=0
    while (v < 9) :                                                        # verticales
        can1.create_line(x1,y1,x2,y2, width = 5 , fill='black')
        x1 = x1 + 70
        x2 = x2 + 70
        v = v + 1       
    while (h < 9) :                                                        #horizontales
        if( h == 0) :
            x1, y1, x2, y2 = 700, 565, -700, 565
        can1.create_line(x1,y1,x2,y2, width = 5 , fill='black')
        y1 = y1 - 70
        y2 = y2 - 70
        h = h + 1



def detruireRegles():
    fen2.destroy
    bou3.configure(state=NORMAL)
       
def regles():
            bou3.configure(state=DISABLED)
            fen2 = Tk()
            fen2.configure(height=500,width=500)
            tex2 = Label(fen2, text='REGLES DU JEU',fg='blue',height=0,width=0)
            tex2.pack(side=TOP)
            txtreg = Label(fen2, text='Othello est un jeu de stratégie à deux joueurs : Noir et Blanc. Il se joue sur un plateau\n unicolore de 64 cases, 8 sur 8, appelé othellier. Ces joueurs disposent de 64 pions\nbicolores, un cote et blancs de l autre. Par commodite, chaque joueur a devant\nlui 32 pions mais ils ne lui appartiennent pas et il doit en donner à son adversaire si\ncelui-ci n en a plus. Un pion est noir si sa face noire est visible et blanc si sa face\nblanche est sur le dessus.   ',fg='black')
            txtreg.pack()              
            bou5 = Button(fen2, text='Fermer fenetre', bg='orange', command = fen2.destroy)            # doit utiliser detruireRegles : bug le bouton Regles ne se desactive pas com prevu apres lavoir clique
            bou5.pack(side=BOTTOM)
            
                   

x1, y1, x2, y2 = 5, 582, 5 , -582                      # coordonees des droites formant la table
fen1 = Tk()
can1 = Canvas(fen1,bg='dark green',height=566.2,width=566.2)
can1.pack(side=LEFT)
tex1 = Label(fen1, text='Bienvenue dans othello!', fg='blue')
tex1.pack()
bou1 = Button(fen1, text='Nouvelle Partie',command = creerTable) 
bou1.pack()
bou2 = Button(fen1, text='Changer couleur plateau',command = couleurAlea)
bou2.pack()
bou3 = Button(fen1, text='Regles du jeu', command = regles)
bou3.pack()
bou4 = Button(fen1, text='Quitter',command = fen1.destroy) 
bou4.pack()
fen1.mainloop()

What i want is this line :

bou5 = Button(fen2, text='Fermer fenetre', bg='orange', command = fen2.destroy)
(in my 4th function)

i want this button to close WIndow 2 and set NORMAL state on button3 with is a window defined in the main program. Probmel is that if i put ( command = detruireRegles ) #(my second fonction)
it says that fen2 is not defined. SO that means i would need to put 2 commands in the command field but i don't know if there is another way then making a function and this one here for me is not working with this methods.

Thanks for your help. Stef

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
You can either pass fen2 as an argument to detruireRegles():
bou5 = Button(fen2, text='Fermer fenetre', bg='orange', command = detruireRegles(fen2))
Then keep detruireRegles the same, or alternatively you can make detruireRegles a locally scoped function, like this:
def regles():
            bou3.configure(state=DISABLED)
            fen2 = Tk()
            fen2.configure(height=500,width=500)
            tex2 = Label(fen2, text='REGLES DU JEU',fg='blue',height=0,width=0)
            tex2.pack(side=TOP)
            txtreg = Label(fen2, text='Othello est un jeu de stratégie à deux joueurs : Noir et Blanc. Il se joue sur un plateau\n unicolore de 64 cases, 8 sur 8, appelé othellier. Ces joueurs disposent de 64 pions\nbicolores, un cote et blancs de l autre. Par commodite, chaque joueur a devant\nlui 32 pions mais ils ne lui appartiennent pas et il doit en donner à son adversaire si\ncelui-ci n en a plus. Un pion est noir si sa face noire est visible et blanc si sa face\nblanche est sur le dessus.   ',fg='black')
            txtreg.pack()  
            def detruireRegles():
                fen2.destroy
                bou3.configure(state=NORMAL)
            bou5 = Button(fen2, text='Fermer fenetre', bg='orange', command = detruireRegles)            # doit utiliser detruireRegles : bug le bouton Regles ne se desactive pas com prevu apres lavoir clique
            bou5.pack(side=BOTTOM)
That should work as well, and is preferable if you're not using that function anywhere else other then in regles.
Wow I changed my sig!

#3
icystef

icystef

    Newbie

  • Members
  • Pip
  • 4 posts
Oh thanks ! First method didn't seem to work really good but the second one is working really good thanks !!

One more question :) You see the purpose of that function was to 'disable' the 'Rules' button in order to forbid the user to open a infinite number of help windows , and then when you hit bou5 it closes help window and reanables the 'Rules buton' on main frame. THe problem is that it works when user exits 'Rules' windows by clicking on bou5 , but if he clicks the X at the top right of the window it will close the window and then the 'Rules' button will remain disabled forever :( Is there a way to name the X button in order for him to do same action as bou5 ?

Thank you very much for your help =)

#4
icystef

icystef

    Newbie

  • Members
  • Pip
  • 4 posts
Solved ! thx anyay =)