my first software

F

FS.Jason

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[0]) + str(done[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[0]) + str(done[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[0]
if len(s) == 3:
y = int(s[1] + s[2])
done.append((x , y))
else:
y = int(s[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\
如果您有任何建议\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[0]) + str(done[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)
mainframe.myCanvas.delete(step)
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()
 
M

march

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

Chelonian

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]
 
J

John Salerno

Chelonian said:
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. :)
 
G

Gabriel Genellina

At said:
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
 
L

Leo Kislov

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[0]) + str(done[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(item[0]) + str(item[1]) for item in done)

for i in range (0, len(s)):
x = s[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
 
S

Steve Holden

Bjoern said:
Fredrik Lundh wrote:




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.
Okay, didn't know that :)

Regards,


Björn

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top