python 来自https://forum.ubuntu-fr.org/viewtopic.php?id=24296

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 来自https://forum.ubuntu-fr.org/viewtopic.php?id=24296相关的知识,希望对你有一定的参考价值。

from Tkinter import *
from random import *
from threading import Thread
import time

class MenuBar(Frame):
    """Barre de menus déroulants"""
    def __init__(self, boss=None):
        Frame.__init__(self, borderwidth =2)
        ##### Menu <Fichier> #####
        fileMenu = Menubutton(self, text ='motif')
        fileMenu.pack(side =LEFT)
        # Partie "déroulante" :
        me1 = Menu(fileMenu)
        me1.add_command(label ='alea', underline =0, command = boss.motif1)
        me1.add_command(label ='rpentomino', underline =0, command = boss.motif2)
        me1.add_command(label ='tpentomino', underline =0, command = boss.motif3)
        me1.add_command(label ='planeur', underline =0, command = boss.motif4)
        me1.add_command(label ='locomotive', underline =0, command = boss.motif6)
        me1.add_command(label ='vaisseau', underline =0, command = boss.motif5)
        me1.add_command(label ='Effacer', underline =0, command = boss.effacer)
        me1.add_command(label ='Terminer', underline =0, command = boss.quit)
        # Intégration du menu :
        fileMenu.configure(menu = me1)
        
class Fenetre(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.geometry("350x350")
        self.master.title(" Jeu de la vie")
        self.jeu =Panneau(self)
        mBar = MenuBar(self.jeu)
        mBar.pack()
        self.jeu.pack(expand =YES, fill=BOTH, padx =0, pady =0)
        self.pack()

class Panneau(Frame, Thread):
    """Panneau de jeu (grille de n x m cases)"""
    def __init__(self, boss =None):
        self.ligne=70
        self.colonne=50
        Frame.__init__(self)
        Thread.__init__(self)
        # Canevas :
        self.can =Canvas(self,width=350, height =350, borderwidth =0)
        # Liaison de l'evenement <clic de souris> a son gestionnaire :
        self.can.bind("<Button-1>", self.clic)
        self.can.pack()
        
        self.cell =[] # construction d'une liste de listes
        self.tabcell=[]
        for i in range(self.ligne): # equivalente a un tableau
            self.cell.append([0]*self.ligne) # de x lignes x x colonnes)
            self.tabcell.append([0]*self.ligne)

         #self.motif("alea")
        self.start()
        
    def motif1(self):
        self.effacer()
        element=[1,0]
        for j in range(self.colonne) :
            for i in range(self.ligne):
                self.cell[i][j]=choice(element)
        self.update()
        
    def motif2(self):
        self.effacer()
        #r-pentomino
        self.cell[26][25]=1
        self.cell[24][26]=1
        self.cell[25][26]=1
        self.cell[26][26]=1
        self.cell[25][27]=1
        self.update()
        
    def motif3(self):
        self.effacer()
        #t-pentomino
        self.cell[24][36]=1
        self.cell[25][36]=1
        self.cell[26][36]=1
        self.cell[25][37]=1
        self.update()
        
    def motif4(self):
        self.effacer()
        #planeur
        self.cell[5][6]=1
        self.cell[6][7]=1
        self.cell[7][5]=1
        self.cell[7][6]=1
        self.cell[7][7]=1
        self.update()
        
    def motif5(self):
        self.effacer()
        #vaisseau spatial
        self.cell[26][26]=1
        self.cell[27][27]=1
        self.cell[23][28]=1
        self.cell[27][28]=1
        self.cell[24][29]=1
        self.cell[25][29]=1
        self.cell[26][29]=1
        self.cell[27][29]=1
        self.update()
        
    def motif6(self):
        self.effacer()
        #locomotive à vapeur
        self.cell[23][26]=1
        self.cell[26][26]=1
        self.cell[27][27]=1
        self.cell[23][28]=1
        self.cell[27][28]=1
        self.cell[24][29]=1
        self.cell[25][29]=1
        self.cell[26][29]=1
        self.cell[27][29]=1

        self.cell[23][33]=1
        self.cell[24][34]=1
        self.cell[25][34]=1
        self.cell[25][35]=1
        self.cell[25][36]=1
        self.cell[24][37]=1
                
        self.cell[23][40]=1
        self.cell[26][40]=1
        self.cell[27][41]=1
        self.cell[23][42]=1
        self.cell[27][42]=1
        self.cell[24][43]=1
        self.cell[25][43]=1
        self.cell[26][43]=1
        self.cell[27][43]=1                
        self.update()

    def update(self):
        for j in range (self.colonne):
               for i in range (self.ligne):
                   if self.cell[i][j]==1:
                       self.tabcell[i][j]=self.can.create_rectangle((i*6),(j*6),(i*6)+4,(j*6)+4,fill="black")
                   else :
                       self.tabcell[i][j]=self.can.create_rectangle((i*6),(j*6),(i*6)+4,(j*6)+4,fill="white")
         
    def run(self):
        while 1:
            for j in range (self.colonne):
                for i in range (self.ligne):
                    if self.cell[i][j]==1:
                        self.can.itemconfigure(self.tabcell[i][j],fill="black")
                    else :
                        self.can.itemconfigure(self.tabcell[i][j],fill="white")
            self.evolution()            
            time.sleep(0.1)
            
    def effacer(self):
        for j in range(self.colonne) :
            for i in range(self.ligne):
                self.cell[i][j]=0
        #effacer !
        self.can.delete(ALL)
        
    def evolution(self):
        #buffer
        self.c =[]
        for i in range(self.ligne): # equivalente a un tableau
            self.c.append([0]*self.ligne) # de x lignes x x colonnes)
            
        #for j in range(self.colonne) :
           # for i in range(self.ligne):
              #  self.c[i][j]=self.cell[i][j] # construction d'une liste de listes
        
        for j in range(self.colonne) :
            for i in range(self.ligne):
                cpt=0
                self.c[i][j]=self.cell[i][j] # remplissage du buffer
                #regles de conway
                #case precedente et suivante
                if self.cell[(self.ligne+i-1)%self.ligne][j]==1:
                    cpt=cpt+1
                if self.cell[(self.ligne+i+1)%self.ligne][j]==1:
                    cpt=cpt+1
                #ligne precedente
                if self.cell[(self.ligne+i-1)%self.ligne][(self.colonne+j-1)%self.colonne]==1:
                    cpt=cpt+1
                if self.cell[i][(self.colonne+j-1)%self.colonne]==1:
                    cpt=cpt+1
                if self.cell[(self.ligne+i+1)%self.ligne][(self.colonne+j-1)%self.colonne]==1:
                    cpt=cpt+1
                #ligne suivante
                if self.cell[(self.ligne+i-1)%self.ligne][(self.colonne+j+1)%self.colonne]==1:
                    cpt=cpt+1		
                if self.cell[i][(self.colonne+j+1)%self.colonne]==1:
                    cpt=cpt+1
                if self.cell[(self.ligne+i+1)%self.ligne][(self.colonne+j+1)%self.colonne]==1:
                    cpt=cpt+1
                #resolution des regles
                if (self.cell[i][j]==1 and (cpt==2 or cpt==3) ) :
                    self.c[i][j]=1
                else :
                    if (self.cell[i][j]==0 and cpt==3) :
                        self.c[i][j]=1
                    else :
                        self.c[i][j]=0
        #self.cell=self.c
        for j in range(self.colonne) :
            for i in range(self.ligne):
                self.cell[i][j]=self.c[i][j] # construction d'une liste de listes
                        
    def clic(self, event):
        #evenement souris
        print "clic clic"        
        
if __name__ == '__main__':
    Fenetre().mainloop()

以上是关于python 来自https://forum.ubuntu-fr.org/viewtopic.php?id=24296的主要内容,如果未能解决你的问题,请参考以下文章

python 来自Python的os文件系统

一封来自“Python”的信

来自 Black Hat Python 书的 Python 嗅探

“路径 python3(来自 --python=python3)不存在”错误

python Python装饰模板(来自“Head First Python ed.2”)

来自嵌套字典的 Python 数据类