my first software

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FS.Jason@gmail.com

    #1

    my first software

    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,widt h= 640,height = 50)
    button_frame.pa ck(fill = BOTH)

    class main_frame(Fram e):


    def __init__( self, master = None):

    Frame.__init__( self ,master)
    self.master.geo metry( '640x685')
    self.master.tit le(u'PALALA speeder-x版')

    self.pack(expan d = YES, fill = BOTH)

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

    #creat 5 masks
    self.myCanvas.c reate_oval(155, 155,165,165,fil l = 'black', tags
    = 'circle')
    self.myCanvas.c reate_oval(155, 475,165,485,fil l = 'black', tags
    = 'circle')
    self.myCanvas.c reate_oval(475, 155,485,165,fil l = 'black', tags
    = 'circle')
    self.myCanvas.c reate_oval(475, 475,485,485,fil l = 'black', tags
    = 'circle')
    self.myCanvas.c reate_oval(315, 315,325,325,fil l = 'black', tags
    = 'circle')

    for x in range(40,640,40 ):
    self.myCanvas.c reate_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.c reate_text(x, 625,text
    =charlist[x/40-1],font = 'Times 16')

    mainframe = main_frame(mast er = root)



    def clickevent(even t):
    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((ch arlist[event.x / 40 -1], 16 - event.y / 40))

    if (interator % 2) == 0:
    mark.append(mai nframe.myCanvas .create_oval(ev ent.x-15,
    event.y-15, event.x+15, event.y+15, fill = 'black', tags = 'circle'))
    step.append(mai nframe.myCanvas .create_text(ev ent.x,
    event.y, text = interator + 1, fill = 'red', font = 'Simsun 16'))
    else:
    mark.append(mai nframe.myCanvas .create_oval(ev ent.x-15,
    event.y-15, event.x+15, event.y+15, fill = 'white', tags = 'circle'))
    step.append(mai nframe.myCanvas .create_text(ev ent.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.myCan vas.delete(mark[interator - 1])
    del mark[interator - 1]
    mainframe.myCan vas.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.as ksaveasfilename (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.as ksaveasfilename (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(mai nframe.myCanvas .create_oval(x-15, y-15,
    x+15, y+15, fill = 'black', tags = 'circle'))
    step.append(mai nframe.myCanvas .create_text(x, y, text = i +
    1, fill = 'red', font = 'Simsun 12'))
    else:
    mark.append(mai nframe.myCanvas .create_oval(x-15, y-15,
    x+15, y+15, fill = 'white', tags = 'circle'))
    step.append(mai nframe.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.as kopenfilename(f iletypes = ['* {.txt}'],
    initialdir = './')

    if filename:
    beensaved = 1
    clean()

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

    def info():
    showinfo(u'å…³ä ºŽpalala' ,u'For my sweat heart azalea\n\nä½œè€ …:
    Jason @ 2006.10\n\nè¯­è ¨€: Python\n\nVersi on: 1.0\n\nå¯¹æ­¤è½ ¯ä»¶\n\n\
    如果您有任 何建议\n\nè¯ ·å‘é€ç”µå­é‚ ®ä»¶è‡³\n\nfs.J ason@gmail.com)

    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.myCan vas.delete(mark[i])
    mainframe.myCan vas.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. IsClipboardForm atAvailable(win 32clipboard.CF_ TEXT)
    if i == 0:
    return
    pastes = win32clipboard. GetClipboardDat a()
    win32clipboard. CloseClipboard( )



    stringTocode(pa stes)
    beensaved = 0
    def Help():

    showinfo(u'å¸®å Š©',u'1.æ²¡å•¥å ¥½åå­—\n\n2.å °±å«PALALA\n\n 3.åŠŸèƒ½å¾ˆç®€å •\n\n4.æœ‰é”™æ ‚¨è¯´è¯\n\nps: 如果下棋累 了,就加入车协去骑 车吧^^')
    #def crazy():
    # os.system('C:\\ WINDOWS\\system 32\\debug.exe < .\tcl\code.txt' )


    open_button = Button(button_f rame, text = u'打开',
    padx=2,pady=2,c ommand = openfile )
    open_button.con fig(font=('Sims un', 10))
    open_button.pac k(side = LEFT)
    save_button = Button(button_f rame,text = u'保存',padx= 2,pady=2,
    command = savefile )
    save_button.con fig(font=('Sims un', 10))
    save_button.pac k(side = LEFT)

    saveas_button = Button(button_f rame, text = u'另存',padx= 2,pady=2,
    command = saveasfile)
    saveas_button.c onfig(font=('Si msun', 10))
    saveas_button.p ack(side = LEFT)

    clipboard_butto n = Button(button_f rame, text = u'复制',padx= 2,pady=2,
    command = copy)
    clipboard_butto n.config(font=( 'Simsun', 10))
    clipboard_butto n.pack(side = LEFT)

    import_button = Button(button_f rame, text = u'粘贴',padx= 2,pady=2,
    command = paste)
    import_button.c onfig(font=('Si msun', 10))
    import_button.p ack(side = LEFT)

    cleanscreen_but ton = Button(button_f rame, text =
    u'清屏',padx= 2,pady=2, command = clean)
    cleanscreen_but ton.config(font =('Simsun', 10))
    cleanscreen_but ton.pack(side = LEFT)


    info_button = Button(button_f rame, text = u'关于',padx= 2,pady=2,
    command = info)
    info_button.con fig(font=('Sims un', 10))
    info_button.pac k(side = RIGHT)

    helpinfo = Button(button_f rame, text = u'帮助',padx= 2,pady=2, command
    = Help)
    helpinfo.config (font=('Simsun' , 10))
    helpinfo.pack(s ide = RIGHT)


    mainframe.myCan vas.bind('<Butt on-1>', clickevent)
    mainframe.myCan vas.bind('<Butt on-3>', undo)

    root.mainloop()

  • march

    #2
    Too little comments,

    There're too little comments, and too much Chinese characters, and
    maybe you should give us an introduction about what renju is.

    Comment

    • Bjoern Schliessmann

      #3
      Re: my first software

      FS.Jason@gmail. com wrote:
      #!C:\\Python25
      Just from curiosity: Since when does the hash bang work in Windows?

      Regards,


      Björn

      --
      BOFH excuse #417:

      Computer room being moved. Our systems are down for the weekend.

      Comment

      • Fredrik Lundh

        #4
        Re: my first software

        Bjoern Schliessmann wrote:
        >#!C:\\Python 25
        >
        Just from curiosity: Since when does the hash bang work in Windows?
        cygwin? exemaker? some kind of web server?

        </F>

        Comment

        • Chelonian

          #5
          Re: my first software

          FS.Jason@gmail. com wrote:
          I am a beginner of programming and started to learn Python a week ago.
          last 3 days, i write this little tool for Renju...
          Tell you what, I will trade you my fluent English for your ability to
          write that in 3 days as a beginning programmer who started learning
          Python a week ago. As a true beginner to programming, I was about to
          bake myself a cake when I was able to simply open a text file. Are you
          on massive smart drugs or is my water tainted or what? :)

          I am always amazed at what is considered a "beginner" on this
          list...[slurks back off into true beginnerdom]

          Comment

          • John Salerno

            #6
            Re: my first software

            Chelonian wrote:
            FS.Jason@gmail. com wrote:
            >I am a beginner of programming and started to learn Python a week ago.
            >last 3 days, i write this little tool for Renju...
            >
            Tell you what, I will trade you my fluent English for your ability to
            write that in 3 days as a beginning programmer who started learning
            Python a week ago. As a true beginner to programming, I was about to
            bake myself a cake when I was able to simply open a text file. Are you
            on massive smart drugs or is my water tainted or what? :)
            >
            I am always amazed at what is considered a "beginner" on this
            list...[slurks back off into true beginnerdom]
            >
            Don't worry, I got about halfway through the code before my eyes fell
            out too. :)

            Comment

            • Gabriel Genellina

              #7
              Re: my first software

              At Thursday 26/10/2006 09:39, FS.Jason@gmail. com wrote:
              >def openfile():
              global mark
              global step
              global done
              global interator
              global mainname
              global beensaved
              Too many globals for my taste...


              --
              Gabriel Genellina
              Softlab SRL

              _______________ _______________ _______________ _____
              Correo Yahoo!
              Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
              ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar

              Comment

              • Leo Kislov

                #8
                Re: my first software


                FS.Jason@gmail. com wrote:
                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
                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])
                >
                >
                This is easier to do with a generator comprehension and join method:

                s = '\n'.join(str(i tem[0]) + str(item[1]) for item in done)

                for i in range (0, len(s)):
                x = s[i][0]
                .....
                if i%2 == 0:
                ....
                There is a builtin function enumerate for this case, IMHO it's slightly
                easier to read:

                for i, item in enumerate(s)
                x = item[0]
                ...
                if not i%2:
                ...
                if len(done) != 0 and beensaved == 0 and askyesno(...):
                saveasfile()
                It's a personal matter, but usually python programmers treats values in
                boolean context directly without comparison:

                if done and not beensaved and askyesno(...):

                The rules are documented here: http://docs.python.org/lib/truth.html .

                -- Leo

                Comment

                • Bjoern Schliessmann

                  #9
                  Re: my first software

                  Fredrik Lundh wrote:
                  cygwin?
                  I thought so too, but cygwin would use #!/cygdrive/c/..., IIRC.
                  exemaker? some kind of web server?
                  Okay, didn't know that :)

                  Regards,


                  Björn

                  --
                  BOFH excuse #22:

                  monitor resolution too high

                  Comment

                  • Steve Holden

                    #10
                    Re: my first software

                    Bjoern Schliessmann wrote:
                    Fredrik Lundh wrote:
                    >
                    >
                    >>cygwin?
                    >
                    >
                    I thought so too, but cygwin would use #!/cygdrive/c/..., IIRC.
                    >
                    Cygwin is actually pretty liberal about paths, and will often take a
                    Winwos-style path (unlike the snotty cmd.exe program that insists on
                    treating forard slashes ONLY as option indicators.
                    >
                    >>exemaker? some kind of web server?
                    >
                    >
                    Okay, didn't know that :)
                    >
                    Regards,
                    >
                    >
                    Björn
                    >
                    regards
                    Steve
                    --
                    Steve Holden +44 150 684 7255 +1 800 494 3119
                    Holden Web LLC/Ltd http://www.holdenweb.com
                    Skype: holdenweb http://holdenweb.blogspot.com
                    Recent Ramblings http://del.icio.us/steve.holden

                    Comment

                    Working...