By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,865 Members | 1,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,865 IT Pros & Developers. It's quick & easy.

my first software

P: n/a
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\
如果您有任何建议\n\n请发送电*邮件 \n\nf*******@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.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(win32cli pboard.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()

Oct 26 '06 #1
Share this Question
Share on Google+
9 Replies


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

Oct 26 '06 #2

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

Regards,
Bjrn

--
BOFH excuse #417:

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

Oct 26 '06 #3

P: n/a
Bjoern Schliessmann wrote:
>#!C:\\Python25

Just from curiosity: Since when does the hash bang work in Windows?
cygwin? exemaker? some kind of web server?

</F>

Oct 26 '06 #4

P: n/a
FS******@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]

Oct 26 '06 #5

P: n/a
Chelonian wrote:
FS******@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. :)
Oct 26 '06 #6

P: n/a
At Thursday 26/10/2006 09:39, FS******@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
Oct 26 '06 #7

P: n/a

FS******@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(item[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

Oct 27 '06 #8

P: n/a
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,
Bjrn

--
BOFH excuse #22:

monitor resolution too high

Oct 27 '06 #9

P: n/a
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,
Bjrn
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

Oct 27 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.