I am a beginner of programming and started to learn Python a week ago.
last 3 days, i write this little tool for Renju.if you have any advice
on my code,please tell me
ps:sorry for my poor english
bow.

#!C:\\Python25
# -*- coding: GBK -*-
from Tkinter import *
import os
import tkFileDialog
import sys
import re
from tkMessageBox import *
import win32clipboard

root = Tk()

interator = 0
done = []
donebac = []
mark = []
step = []
beensaved = 0
code = ''
pastes = ''
mainname = ''
button_frame = Frame(root,width= 640,height = 50)
button_frame.pack(fill = BOTH)

class main_frame(Frame):


    def __init__( self, master = None):

        Frame.__init__( self ,master)
        self.master.geometry( '640x685')
        self.master.title(u'PALALA   speeder-x版')

        self.pack(expand = YES, fill = BOTH)

        self.myCanvas = Canvas(self, bg = '#f8dc59')
        self.myCanvas.pack(expand = YES, fill = BOTH)
        for x in range(40, 640, 40 ):
            for y in range(40,640,40):
                self.myCanvas.create_oval(x,y,x,640-y,fill = 'black')
                self.myCanvas.create_oval(x,y,640-x,y,fill = 'black')

         #creat 5 masks
        self.myCanvas.create_oval(155,155,165,165,fill = 'black', tags
= 'circle')
        self.myCanvas.create_oval(155,475,165,485,fill = 'black', tags
= 'circle')
        self.myCanvas.create_oval(475,155,485,165,fill = 'black', tags
= 'circle')
        self.myCanvas.create_oval(475,475,485,485,fill = 'black', tags
= 'circle')
        self.myCanvas.create_oval(315,315,325,325,fill = 'black', tags
= 'circle')

        for x in range(40,640,40):
            self.myCanvas.create_text(15, x, text = 16-x/40 ,font =
'Times 16')
        for x in range(40,640,40):
            charlist =
['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O']
            self.myCanvas.create_text(x, 625,text
=charlist[x/40-1],font = 'Times 16')

mainframe = main_frame(master = root)



def clickevent(event):
    global interator
    global done
    global mark
    global step
    global beensaved
    if 20 < event.x < 620 and 20 < event.y < 620:
         event.x = event.x + 20 - (event.x + 20) % 40
         event.y = event.y + 20 - (event.y + 20) % 40

         charlist =
['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O']

         for item in done:
            if item ==  (charlist[event.x / 40 -1], 16 - event.y / 40):

                return
            print item

         done.append((charlist[event.x / 40 -1], 16 - event.y / 40))

         if (interator % 2) == 0:
             mark.append(mainframe.myCanvas.create_oval(event.x-15,
event.y-15, event.x+15, event.y+15, fill = 'black', tags = 'circle'))
             step.append(mainframe.myCanvas.create_text(event.x,
event.y, text = interator + 1, fill = 'red', font = 'Simsun 16'))
         else:
             mark.append(mainframe.myCanvas.create_oval(event.x-15,
event.y-15, event.x+15, event.y+15, fill = 'white', tags = 'circle'))
             step.append(mainframe.myCanvas.create_text(event.x,
event.y, text = interator + 1, fill = 'red', font = 'Simsun 16'))



         interator = interator + 1
         print done
    beensaved = 0
def undo(event):

    global interator
    global mark
    global step
    global done
    global beensaved
    if interator > 0:
         mainframe.myCanvas.delete(mark[interator - 1])
         del mark[interator - 1]
         mainframe.myCanvas.delete(step[interator - 1])
         del step[interator - 1]
         del done[interator - 1]
         interator = interator - 1
    beensaved = 0
def savefile():

    global done
    global donebac
    global mainname
    global beensaved

    if done == []:
        info_blank()
        return

    if beensaved == 0 and mainname == '':
        filename = tkFileDialog.asksaveasfilename(filetypes =
[('savefile','*.txt')], initialdir = './')
    else:
        filename = mainname

    s = ''

    for i in range (0,len(done) - 1):
        s = s +str(done[i][0]) + str(done[i][1]) + '\n'
    s = s + str(done[len(done) - 1][0]) + str(done[len(done) - 1][1])



    if filename:
        filename = re.sub('.txt','',filename)
        savefile = open(filename + '.txt', 'w')
        savefile.write(s)
        savefile.close()
        beensaved = 1
        mainname = filename
        donebac = done
def saveasfile():
    global done
    global donebac
    global mainname
    global beensaved

    if done == []:
        info_blank()
        return

    filename = tkFileDialog.asksaveasfilename(filetypes =
[('savefile','*.txt')], initialdir = './')
    s = ''

    for i in range (0,len(done) - 1):
        s = s +str(done[i][0]) + str(done[i][1]) + '\n'
    s = s + str(done[len(done) - 1][0]) + str(done[len(done) - 1][1])

    if filename:
        filename = re.sub('.txt','',filename)
        savefile = open(filename + '.txt', 'w')
        savefile.write(s)
        savefile.close()
        beensaved = 1
        mainname = filename
        donebac = done

def stringTocode(s):
    global mark
    global step
    global done
    global interator
    global mainname
    global beensaved
    s = s.upper()
    mistake1 = re.compile('[P-Z]+')
    if mistake1.search(s):

showinfo(u'棋谱错误',u'棋谱里只能有A-O,仔细检查一下先^^')
        return
    mistake2 = re.compile('[A-O]+[A-O]+')
    if mistake2.search(s):

showinfo(u'棋谱错误',u'ft,两个字母连在一起了???!!')
        return
    mistake3 = re.compile('[A-O]+0')
    if mistake3.search(s):
        showinfo(u'棋谱错误',u'字母后面跟0?写错了吧^^')
        return
    s = re.sub(' ','',s)
    s = re.sub('[0-9]+[0-9]*[0-9]*[,.]+','',s)
    s = re.sub('\W','',s)
    #s = re.sub('[a-z]','[A-Z]', s)
    search = re.compile('[A-O]+[0-9]+[0-5]*')
    s = search.findall(s)
    interator = len(s)
    charlist =
['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O']

    for i in range (0, len(s)):
        x = s[i][0]
        if len(s[i]) == 3:
            y = int(s[i][1] + s[i][2])
            done.append((x , y))
        else:
            y = int(s[i][1])
            done.append((x , y))

        for j in range(0, len(charlist)):
            if x == charlist[j]:
                x = j
        x = (x + 1) * 40
        y = (16 - y) * 40

        if i%2 == 0:
            mark.append(mainframe.myCanvas.create_oval(x-15, y-15,
x+15, y+15, fill = 'black', tags = 'circle'))
            step.append(mainframe.myCanvas.create_text(x, y, text = i +
1, fill = 'red', font = 'Simsun 12'))
        else:
            mark.append(mainframe.myCanvas.create_oval(x-15, y-15,
x+15, y+15, fill = 'white', tags = 'circle'))
            step.append(mainframe.myCanvas.create_text(x, y, text = i +
1, fill = 'red', font = 'Simsun 12'))
    print done


def openfile():
    global mark
    global step
    global done
    global interator
    global mainname
    global beensaved

    if len(done) != 0 and beensaved == 0 and
askyesno(u'提示',u'是否保存当前棋谱?'):
            saveasfile()

    filename = tkFileDialog.askopenfilename(filetypes = ['* {.txt}'],
initialdir = './')

    if filename:
        beensaved = 1
        clean()

        openedfile = open(filename, 'r')
        openedfile.seek(0)
        filecon = openedfile.read()
        stringTocode(filecon)
        mainname = filename
        beensaved = 1

def info():
    showinfo(u'关于palala' ,u'For my sweat heart azalea\n\n作者:
Jason  @ 2006.10\n\n语言: Python\n\nVersion: 1.0\n\n对此软件\n\n\
[EMAIL PROTECTED])

def info_blank():
    showinfo(u'-,-',u'还未走一步棋')

def copy():

    if done == []:
        info_blank()
        return

    s = ''

    for i in range (0,len(done) - 1):
        s = s +str(done[i][0]) + str(done[i][1]) + '\n'
    s = s + str(done[len(done) - 1][0]) + str(done[len(done) - 1][1])

    root.clipboard_clear()
    root.clipboard_append(s)

def clean():
    global done
    global beensaved
    global mark
    global step
    global interator

    if len(done) != 0 and beensaved == 0 and
askyesno(u'提示',u'是否保存当前棋谱?'):
            saveasfile()
    for i in range (0, interator):
        mainframe.myCanvas.delete(mark[i])
        mainframe.myCanvas.delete(step[i])
    mark = []
    step = []
    done = []
    interator = 0
    beensaved = 1

def paste():
    global pastes
    global done
    global beensaved
    if len(done) != 0 and beensaved == 0 and
askyesno(u'提示',u'是否保存当前棋谱?'):
            saveasfile()


    beensaved = 1
    clean()

    win32clipboard.OpenClipboard(0)
    i =
win32clipboard.IsClipboardFormatAvailable(win32clipboard.CF_TEXT)
    if i == 0:
        return
    pastes = win32clipboard.GetClipboardData()
    win32clipboard.CloseClipboard()



    stringTocode(pastes)
    beensaved = 0
def Help():

showinfo(u'帮助',u'1.没啥好名字\n\n2.就叫PALALA\n\n3.功能很简单\n\n4.有错您说话\n\nps:如果下棋累了,就加入车协去骑车吧^^')
#def crazy():
 #   os.system('C:\\WINDOWS\\system32\\debug.exe < .\tcl\code.txt')


open_button = Button(button_frame, text = u'打开',
padx=2,pady=2,command = openfile )
open_button.config(font=('Simsun', 10))
open_button.pack(side = LEFT)
save_button = Button(button_frame,text = u'保存',padx=2,pady=2,
command = savefile )
save_button.config(font=('Simsun', 10))
save_button.pack(side = LEFT)

saveas_button = Button(button_frame, text = u'另存',padx=2,pady=2,
command = saveasfile)
saveas_button.config(font=('Simsun', 10))
saveas_button.pack(side = LEFT)

clipboard_button = Button(button_frame, text = u'复制',padx=2,pady=2,
command = copy)
clipboard_button.config(font=('Simsun', 10))
clipboard_button.pack(side = LEFT)

import_button = Button(button_frame, text = u'粘贴',padx=2,pady=2,
command = paste)
import_button.config(font=('Simsun', 10))
import_button.pack(side = LEFT)

cleanscreen_button = Button(button_frame, text =
u'清屏',padx=2,pady=2, command = clean)
cleanscreen_button.config(font=('Simsun', 10))
cleanscreen_button.pack(side = LEFT)


info_button = Button(button_frame, text = u'关于',padx=2,pady=2,
command = info)
info_button.config(font=('Simsun', 10))
info_button.pack(side = RIGHT)

helpinfo = Button(button_frame, text = u'帮助',padx=2,pady=2, command
= Help)
helpinfo.config(font=('Simsun', 10))
helpinfo.pack(side = RIGHT)


mainframe.myCanvas.bind('<Button-1>', clickevent)
mainframe.myCanvas.bind('<Button-3>', undo)

root.mainloop()

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to