#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    [Tkinter] Need help in my game


    Hi, i am creating a game with python tkinter and i got stuck on a part that i dont know what to do.

    What i want to do is: when i press OK on Image 1, some of the attributes (Name, chosen race, Lvl...) that was created on that window be sent to Image 2. In oder words, i want to be able to change a attribute from App for a attribute from Newgame after pressing the OK button. Ex: make self.char_name = self.name, self.char_lvl = self.lvl, self.char_exp = self.exp

    Hope you guys can help me

    Code:
    # -*- coding: cp1252 -*-
    from Tkinter import *
    from tkFont import *
    import time
    
    #tela do New Game
    class NewGame:
        def __init__(self,parent,titulo):
            n = self.nova = Toplevel(parent)
            n.title(titulo)
            Label(n, text = titulo).pack()
            self.y = 0
    #imput do nome
    ## colocar um limitante de numero de letras
            F0 = Frame(n)
            F0.pack(side = TOP,anchor = NW,padx = 6)
            l3 = Label(F0, text = 'Name:')
            l3.pack(side = TOP,anchor = NW,padx = 6)
            t4 = Text(F0)
            t4.pack(side = TOP,anchor = NW,padx = 6)
            t4.config(height = 1, width = 15)
            self.t4 = t4
            
            F1 = Frame(n)
            F1.pack(side = LEFT,anchor = NW)
            l1 = Label(F1, text = 'Race')
            l1.grid(row = 0,column = 0,padx = 10,pady = 1)
            l1.config(width = 10)
            
            F2 = Frame(n)
            F2.pack(side = LEFT,anchor = NW)
            f1 = Frame(F2)
            f1.pack(side = TOP,anchor = NW)
            f2 = Frame(F2)
            f2.pack(side = TOP,anchor = NW)
            l2 = Label(f1, text = 'Class')
            l2.grid(row = 0,column = 1,padx = 10,pady = 1)
            l2.config(width = 10)
            
            F4 = Frame(n)
            F4.pack(side = BOTTOM,anchor = NW)
            b1 = Button(F4,text = 'OK',command = self.ok,width = 10)
            b1.grid(row = 0,column = 0,padx = 5)
            b2 = Button(F4,text = 'Close',command = self.close,width = 10)
            b2.grid(row = 0,column = 2,padx = 5)
            
            self.f2 = f2
            self.F2 = F2
    #lista dos botões de raça        
            self.cria_botao(F1,'Human',1,0)
            self.cria_botao(F1,'Elf',2,0)
            self.cria_botao(F1,'Dark Elf',3,0)
            self.cria_botao(F1,'Orc',4,0)
            self.cria_botao(F1,'Dwarf',5,0)
    #cria o visor onde aparece a raca escolhida
            self.t1 = Text(F1)
            self.t1.grid(row = 6,column = 0,padx = 10,pady = 1)
            self.t1.config(state=DISABLED,height =1,width=10)
    #cria o visor onde estão os status
            F3 = Frame(n)
            F3.pack(side = LEFT,anchor = NW)
            l3 = Label(F3, text = 'Status')
            l3.pack(side = TOP,anchor = NW)
            self.t3 = Text(F3)
            self.t3.pack(side = TOP,anchor = NW)
            self.t3.config(state=DISABLED,height = 2,width = 31)
            
        def ok(self):
            self.clase = self.t2.get(1.0,END)
            self.race = self.t1.get(1.0,END)
            self.name = self.t4.get(1.0,END)
            self.lvl = 1
            self.exp = 0
            self.nova.destroy()
    
        def close(self):
            self.nova.destroy()
    
        def add_texto(self,escrito):
            self.t3.config(state=NORMAL)
            self.t3.delete(1.0,END)
            self.t3.insert(INSERT,escrito)
            self.t3.config(state=DISABLED)
            
        def cria_botao(self,frame,texto,linha,coluna):
            self.b = Button(frame,text = texto,command=lambda x=texto:self.botao(x),width=10)  
            self.b.grid(row = linha,column = coluna,padx = 10,pady = 1)
        def botao(self,x):
    #bloco da resposta dos botões de raça e classe
    ##ainda a ser definido os status, classes e nomes
            if x == 'Human':
                if self.y <> 0:
                    self.f2.destroy()  
                self.STR1 = 10
                self.CON1 = 10
                self.DEX1 = 10
                self.INT1 = 10
                self.MEN1 = 10
                self.WIT1 = 10
                self.CHA1 = 10
                self.LUK1 = 10
                self.t1.config(state=NORMAL)
                self.t1.delete(1.0,END)
                self.t1.insert(END,x)
                self.t1.config(state=DISABLED)
                self.add_texto('SRT: %0.0f'%self.STR1+' CON: %0.0f'%self.CON1+' DEX: %0.0f'%self.DEX1+' CHA: %0.0f'%self.CHA1+'\n'+'INT: %0.0f'%self.INT1+' MEN: %0.0f'%self.MEN1+' WIT: %0.0f'%self.WIT1+' LUK: %0.0f'%self.LUK1)
                self.classes = ['Knight','Warrior','Archer','Rogue','Wizard','Summoner','Enchanter','Healer']
                c = 1
                self.f2 = Frame(self.F2)
                self.f2.pack(side = TOP,anchor = NW)
                for i in self.classes:
                    self.cria_botao(self.f2,i,c,0)
                    c = c + 1
                self.t2 = Text(self.f2)
                self.t2.grid(row = c,column = 0,padx = 10,pady = 1)
                self.t2.config(state=DISABLED,height =1,width=10)
                self.y = 1
            if x == 'Elf':
                if self.y <> 0:
                    self.f2.destroy()
                self.STR2 = 10
                self.CON2 = 10
                self.DEX2 = 10
                self.INT2 = 10
                self.MEN2 = 10
                self.WIT2 = 10
                self.CHA2 = 10
                self.LUK2 = 10
                self.t1.config(state=NORMAL)
                self.t1.delete(1.0,END)
                self.t1.insert(END,x)
                self.t1.config(state=DISABLED)
                self.add_texto('SRT: %0.0f'%self.STR2+' CON: %0.0f'%self.CON2+' DEX: %0.0f'%self.DEX2+' CHA: %0.0f'%self.CHA2+'\n'+'INT: %0.0f'%self.INT2+' MEN: %0.0f'%self.MEN2+' WIT: %0.0f'%self.WIT2+' LUK: %0.0f'%self.LUK2)
                self.classes = ['Knight','Warrior','Archer','Rogue','Wizard','Summoner','Enchanter','Healer']
                c = 1
                self.f2 = Frame(self.F2)
                self.f2.pack(side = TOP,anchor = NW)
                for i in self.classes:
                    self.cria_botao(self.f2,i,c,0)
                    c = c + 1
                self.t2 = Text(self.f2)
                self.t2.grid(row = c,column = 0,padx = 10,pady = 1)
                self.t2.config(state=DISABLED,height =1,width=10)
                self.y = 1
            if x == 'Dark Elf':
                if self.y <> 0:
                    self.f2.destroy()
                self.STR3 = 10
                self.CON3 = 10
                self.DEX3 = 10
                self.INT3 = 10
                self.MEN3 = 10
                self.WIT3 = 10
                self.CHA3 = 10
                self.LUK3 = 10
                self.t1.config(state=NORMAL)
                self.t1.delete(1.0,END)
                self.t1.insert(END,x)
                self.t1.config(state=DISABLED)
                self.add_texto('SRT: %0.0f'%self.STR3+' CON: %0.0f'%self.CON3+' DEX: %0.0f'%self.DEX3+' CHA: %0.0f'%self.CHA3+'\n'+'INT: %0.0f'%self.INT3+' MEN: %0.0f'%self.MEN3+' WIT: %0.0f'%self.WIT3+' LUK: %0.0f'%self.LUK3)
                self.classes = ['Knight','Warrior','Archer','Rogue','Wizard','Summoner','Enchanter','Healer']
                c = 1
                self.f2 = Frame(self.F2)
                self.f2.pack(side = TOP,anchor = NW)
                for i in self.classes:
                    self.cria_botao(self.f2,i,c,0)
                    c = c + 1
                self.t2 = Text(self.f2)
                self.t2.grid(row = c,column = 0,padx = 10,pady = 1)
                self.t2.config(state=DISABLED,height =1,width=10)
                self.y = 1
            if x == 'Orc':
                if self.y <> 0:
                    self.f2.destroy()
                self.STR4 = 10
                self.CON4 = 10
                self.DEX4 = 10
                self.INT4 = 10
                self.MEN4 = 10
                self.WIT4 = 10
                self.CHA4 = 10
                self.LUK4 = 10
                self.t1.config(state=NORMAL)
                self.t1.delete(1.0,END)
                self.t1.insert(END,x)
                self.t1.config(state=DISABLED)
                self.add_texto('SRT: %0.0f'%self.STR4+' CON: %0.0f'%self.CON4+' DEX: %0.0f'%self.DEX4+' CHA: %0.0f'%self.CHA4+'\n'+'INT: %0.0f'%self.INT4+' MEN: %0.0f'%self.MEN4+' WIT: %0.0f'%self.WIT4+' LUK: %0.0f'%self.LUK4)
                self.classes = ['Warrior','Summoner','Enchanter','Healer']
                c = 1
                self.f2 = Frame(self.F2)
                self.f2.pack(side = TOP,anchor = NW)
                for i in self.classes:
                    self.cria_botao(self.f2,i,c,0)
                    c = c + 1
                self.t2 = Text(self.f2)
                self.t2.grid(row = c,column = 0,padx = 10,pady = 1)
                self.t2.config(state=DISABLED,height =1,width=10)
                self.y = 1
            if x == 'Dwarf':
                if self.y <> 0:
                    self.f2.destroy()
                self.STR5 = 10
                self.CON5 = 10
                self.DEX5 = 10
                self.INT5 = 10
                self.MEN5 = 10
                self.WIT5 = 10
                self.CHA5 = 10
                self.LUK5 = 10
                self.t1.config(state=NORMAL)
                self.t1.delete(1.0,END)
                self.t1.insert(END,x)
                self.t1.config(state=DISABLED)
                self.add_texto('SRT: %0.0f'%self.STR5+' CON: %0.0f'%self.CON5+' DEX: %0.0f'%self.DEX5+' CHA: %0.0f'%self.CHA5+'\n'+'INT: %0.0f'%self.INT5+' MEN: %0.0f'%self.MEN5+' WIT: %0.0f'%self.WIT5+' LUK: %0.0f'%self.LUK5)
                self.classes = ['Knight','Warrior']
                c = 1
                self.f2 = Frame(self.F2)
                self.f2.pack(side = TOP,anchor = NW)
                for i in self.classes:
                    self.cria_botao(self.f2,i,c,0)
                    c = c + 1
                self.t2 = Text(self.f2)
                self.t2.grid(row = c,column = 0,padx = 10,pady = 1)
                self.t2.config(state=DISABLED,height =1,width=10)
                self.y = 1
    
            if x == 'Knight':
                self.t2.config(state=NORMAL)
                self.t2.delete(1.0,END)
                self.t2.insert(END,x)
                self.t2.config(state=DISABLED)
            if x == 'Warrior':
                self.t2.config(state=NORMAL)
                self.t2.delete(1.0,END)
                self.t2.insert(END,x)
                self.t2.config(state=DISABLED)
            if x == 'Archer':
                self.t2.config(state=NORMAL)
                self.t2.delete(1.0,END)
                self.t2.insert(END,x)
                self.t2.config(state=DISABLED)
            if x == 'Wizard':
                self.t2.config(state=NORMAL)
                self.t2.delete(1.0,END)
                self.t2.insert(END,x)
                self.t2.config(state=DISABLED)
            if x == 'Healer':
                self.t2.config(state=NORMAL)
                self.t2.delete(1.0,END)
                self.t2.insert(END,x)
                self.t2.config(state=DISABLED)
    
            
            
    class App:
        def __init__(self):
    #cria janela e titulo
            j = self.janela = Tk()
            j.title('Jogo')
            j.geometry('850x470')
    
    #não permite que se abra 2 new games ao mesmo tempo
    ##fazer com que quando o new game seja fechado este termo volte a ser 0
            self.contador = 0
        
    #Menu
            menu_bar = Menu(j)
            
            menu_menu = Menu(menu_bar, tearoff=0)
            menu_menu.add_command(label='New Game', command=self.new_game)
            menu_menu.add_separator()
            menu_menu.add_command(label='Load Game', command=self.load_game)
            menu_menu.add_command(label='Save Game', command=self.save_game)
            menu_menu.add_separator()
            menu_menu.add_command(label='Quit', command=self.quit_game)
    
            menu_bar.add_cascade(label='Menu', menu=menu_menu)
    
            j.config(menu=menu_bar)
    
    #Barra de Status
            F1 = Frame(j)
            F1.pack(anchor = NW)
            f2 = Frame(F1)
            f2.pack(side = LEFT,anchor = NW)
            self.char_name = ''
            self.char_lvl = ''
            self.char_hp = ''
            self.char_mp = ''
            self.char_exp = ''
            self.char_sp = ''
            self.cria_texto(f2,'Name: '+ self.char_name,0,0)
            self.cria_texto(f2,'LvL: '+ self.char_lvl,1,0)
            self.cria_texto(f2,'HP: '+ self.char_hp,0,1)
            self.cria_texto(f2,'MP: '+ self.char_mp,0,2)
            self.cria_texto(f2,'Exp: '+ self.char_exp,1,1)
            self.cria_texto(f2,'SP: '+ self.char_sp,1,2)
    #Barra de Texto
            F2 = Frame(j)
            F2.pack(anchor = NW)
            scrollbar = Scrollbar(F2)
            scrollbar.pack(side = RIGHT,fill = Y)
            f1 = Text(F2,yscrollcommand = scrollbar.set)
            scrollbar.config(command = f1.yview)
    #       self.add_texto(f1,'texto')
            f1.config(state=DISABLED,width=50)
            f1.pack(side=LEFT)
    
    
    
        def quit_game(self):
            self.janela.quit()
            self.janela.destroy()
    
        def new_game(self):
            if self.contador == 0:
                diag = NewGame(self.janela,'Character Creation')
                self.contador = 1
            else:
                pass
    
        def load_game(self):
    #definir um leitor de um arquivo texto onde estara já definido o que se deve
    #pegar e adiciona esses dados em seus devidos locais na tela do jogo
            print 'por fazer'
    
        def save_game(self):
    #definir os dados a serem salvos e envialos para um arquivo texto
    #se possivel, colocar este arquivo de forma que o usuario não possa alteralo
            print 'por fazer'
    
        def roda(self):
            self.janela.mainloop()
    
        def texto(self,escrito):
    #tudo que for colocado na lista de texto usara esse comando
            localtime = time.asctime( time.localtime(time.time()) )
            escreve = localtime[11:19]
            escreve = escreve + ' - ' + escrito + "\n"
            return escreve
        def add_texto(self,frame,escrito):
            frame.insert(INSERT, self.texto(escrito))
    
        def cria_texto(self,frame,texto,linha,coluna):
            t = Label(frame,text = texto)
            t.grid(row = linha, column = coluna, padx = 10, pady = 10,sticky = W)
    
    Inicio = App()
    Inicio.roda()
    image 1


    image 2
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,540
    Rep Power
    1906
    Can see you already pass the Tk window (janela) to the new game.

    If you want to access more, you have to extend what you pass on.
    To show what I mean, I made the following change which "add" a name when you select 'new game' in the menu.

    1. Make the standard GUI accessible through 'self'
    Code:
            f2 = Frame(F1)
            f2.pack(side = LEFT,anchor = NW)
            self.myf2 = f2 #NEW
    2. add the self as parameter and pass it to newGame:
    Code:
    diag = NewGame(self.janela,'Character Creation',self) # NEW The last parameter
    3. add argument to get the "mainFrame" in newGame and then call what you need to change
    Code:
    class NewGame:
        def __init__(self,parent,titulo,mainFrame):
            mainFrame.cria_texto(mainFrame.myf2,'Name: me',0,0)
    You will of course have to modify to your needs, e.g. first make the changes when the user press ok.
    I hope this gave you an idea how to proceed.

IMN logo majestic logo threadwatch logo seochat tools logo