943,995 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1869
  • Python RSS
Jan 29th, 2007
0

Can't get grid to work!

Expand Post »
What am I doing wrong, I've done it before!

Python Syntax (Toggle Plain Text)
  1. class Main:
  2. def __init__(self, master):
  3. self.master = master
  4. self.master.title('Role Playing Form V1.0')
  5. self.master.geometry('300x250+350+450')
  6. self.master.mainloop()
  7. self.cmdCreate = Button(self.master, text='Create Character', command=self.create)
  8. self.cmdCreate.grid(row=0)
  9. def create(self):
  10. pass
  11. root = Tk()
  12. main=Main(root)

the button doesn't appear!
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Jan 29th, 2007
0

Re: Can't get grid to work!

NVM fixed
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Jan 29th, 2007
0

Re: Can't get grid to work!

Just in case others may wonder, you put the event loop mainloop() in the wrong place:
Python Syntax (Toggle Plain Text)
  1. from Tkinter import *
  2.  
  3. class Main:
  4. def __init__(self, master):
  5. self.master = master
  6. self.master.title('Role Playing Form V1.0')
  7. self.master.geometry('300x250+350+450')
  8. #self.master.mainloop() #!!!!!!!!!!
  9. self.cmdCreate = Button(self.master, text='Create Character', command=self.create)
  10. self.cmdCreate.grid(row=0)
  11. def create(self):
  12. pass
  13.  
  14. root = Tk()
  15. main=Main(root)
  16. # put event loop here
  17. root.mainloop()
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Jan 30th, 2007
0

Re: Can't get grid to work!

Another problem. I can't write to an existing file. I want it to create a file with the characterName, and read the same file again. However it just creates a new file with a blank title, and writes the data there. Here's the code:

Python Syntax (Toggle Plain Text)
  1. #Role Playing Form.py
  2. from Tkinter import *
  3. global Name
  4. global className
  5. class Main:
  6. def __init__(self, master):
  7. self.master = master
  8. self.master.title('Role Playing Form V1.0')
  9. self.master.geometry('250x100+350+450')
  10. self.cmdCreate = Button(self.master, text='Create Character', command=self.create)
  11. self.cmdCreate.grid(row=0)
  12. self.cmdDelete = Button(self.master, text='Delete Character', command=self.delete)
  13. self.cmdDelete.grid(row=1)
  14. self.cmdLoad = Button(self.master, text='Load Character', command=self.load)
  15. self.cmdLoad.grid(row=2)
  16. self.master.mainloop()
  17. def create(self):
  18. nameCreate()
  19. def delete(self):
  20. pass
  21. def load(self):
  22. pass
  23. #Brings up a dialog box for putting the name in.
  24. #The enter button creates a file with name.txt for the title.
  25. #This will be used to store data for that character.
  26. class nameCreate:
  27. global Name
  28. def __init__(self):
  29. self.nameInput = Toplevel(root)
  30. self.nameInput.title('Name of Character?')
  31. self.nameInput.geometry('300x100+350+450')
  32. self.lblName = Label(self.nameInput, text='Enter Name Here')
  33. self.lblName.grid(row=0)
  34. self.entName = Entry(self.nameInput)
  35. self.entName.grid(row=0, column=1)
  36. self.cmdName = Button(self.nameInput, text='Enter', command=self.enter)
  37. self.cmdName.grid(row=1)
  38. def enter(self):
  39. Name = self.entName.get()
  40. print "Character Name:",Name
  41. fileChar = open('Character Data\ '+Name+'.txt',"w")
  42. classCreate()
  43. self.nameInput.destroy()
  44. class classCreate:
  45. global className
  46. global Name
  47. def __init__(self):
  48. self.classChoose = Toplevel(root)
  49. self.classChoose.title('Choose your Class')
  50. self.classChoose.geometry('300x200+300+400')
  51. self.labelChoose = Label(self.classChoose, text="Select the class you wish to be")
  52. self.labelChoose.grid(row=0)
  53. self.listClass = Listbox(self.classChoose, height=4)
  54. self.listClass.grid(row=1)
  55. self.listClass.insert(END,"Warrior")
  56. self.confirm = Button(self.classChoose, text="Confirm", command=self.confirm)
  57. self.confirm.grid(row=2)
  58. def confirm(self):
  59. fileClass = open('ClassData.txt','r')
  60. for line in fileClass:
  61. classInfo=line.split(',')
  62. print Name
  63. print "This class's stats are:",classInfo
  64. className = classInfo[0]
  65. print "The name of the class is:",className
  66. fileChar = open('Character Data\ '+Name+'.txt','a')
  67. fileChar.writelines(className+',')
  68. root = Tk()
  69. main=Main(root)
Why is 'Name' changing to "", even though I called it global?
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Jan 30th, 2007
0

Re: Can't get grid to work!

Ok, well it writes correctly, but how do I make it write to the file on the next line down? And then read it?
The file will look like this:

Stats,Morestats,,,,,,,,
ExpGot,ExpToGain

How do I make it write to the next line after it has finished the initial one?

I also want it to read the line seperately from the firstHere's the code:

[php]#Role Playing Form.py
from Tkinter import *
#Define Global Variables
global Name
global CharHpMax
global CharHpCurrent
global CharMpMax
global CharMpCurrent
global CharStr
global CharDef
global CharMAtt
global CharMDef
global Currentexp
global Requiredexp
#Create the Start window
class Main:
def __init__(self, master):
torch=PhotoImage(file="C:\Python24\Python Progs\Role Playing Form\Images\Title Screen Flames.gif")
grave=PhotoImage(file="C:\Python24\Python Progs\Role Playing Form\Images\TitleScreenGraves.gif")
ankh=PhotoImage(file="C:\Python24\Python Progs\Role Playing Form\Images\TitleScreenAnkh.gif")
self.master = master
self.master.title('Role Playing Form V1.0')
self.master.geometry('250x250+350+450')
self.torchLabelL=Label(self.master, image=torch)
self.torchLabelL.grid(row=0, column=0)
self.cmdCreate = Button(self.master, text='Create Character', command=self.create)
self.cmdCreate.grid(row=0, column=1)
self.torchLabelR=Label(self.master, image=torch)
self.torchLabelR.grid(row=0, column=2)
self.graveLabelL=Label(self.master, image=grave)
self.graveLabelL.grid(row=1, column=0)
self.cmdDelete = Button(self.master, text='Delete Character', command=self.delete)
self.cmdDelete.grid(row=1, column=1)
self.graveLabelR=Label(self.master, image=grave)
self.graveLabelR.grid(row=1, column=2)
self.ankhLabelL=Label(self.master, image=ankh)
self.ankhLabelL.grid(row=2, column=0)
self.cmdLoad = Button(self.master, text='Load Character', command=self.load)
self.cmdLoad.grid(row=2, column=1)
self.ankhLabelR=Label(self.master, image=ankh)
self.ankhLabelR.grid(row=2, column=2)
self.master.mainloop()
def create(self):
nameCreate()
def delete(self):
pass
def load(self):
nameLoad()
#Brings up a dialog box for putting the name in.
#The enter button creates a file with name.txt for the title.
#This will be used to store data for that character.
class nameCreate:
def __init__(self):
self.nameInput = Toplevel(root)
self.nameInput.title('Name of Character?')
self.nameInput.geometry('300x100+350+450')
self.lblName = Label(self.nameInput, text='Enter Name Here')
self.lblName.grid(row=0)
self.entName = Entry(self.nameInput)
self.entName.grid(row=0, column=1)
self.cmdName = Button(self.nameInput, text='Enter', command=self.enter)
self.cmdName.grid(row=1)
def enter(self):
global Name
Name = self.entName.get()
print "Character Name:",Name
fileChar = open('Character Data\ '+Name+'.txt',"w")
classCreate()
self.nameInput.destroy()
class nameLoad:
def __init__(self):
self.nameInput = Toplevel(root)
self.nameInput.title('Name of Character?')
self.nameInput.geometry('300x100+350+450')
self.lblName = Label(self.nameInput, text='Enter Name Here')
self.lblName.grid(row=0)
self.entName = Entry(self.nameInput)
self.entName.grid(row=0, column=1)
self.cmdName = Button(self.nameInput, text='Load', command=self.load)
self.cmdName.grid(row=1)
def load(self):
global Name
global CharHpMax
global CharHpCurrent
global CharMpMax
global CharMpCurrent
global CharStr
global CharDef
global CharMAtt
global CharMDef
global Currentexp
global Requiredexp
Name = self.entName.get()
fileChar = open('Character Data\ '+Name+'.txt','r')
for line in fileChar:
classInfo=line.split(',')
CharHpMax = classInfo[1]
CharHpCurrent = classInfo[2]
CharMpMax = classInfo[3]
CharMpCurrent = classInfo[4]
CharStr = classInfo[5]
CharDef = classInfo[6]
CharMAtt = classInfo[7]
CharMDef = classInfo[8]
#Stuff about exp here assigned to the Current and Required
#exp stats
mainGame()

#Adds the stats based on what you selected in the form
class classCreate:
def __init__(self):
self.classChoose = Toplevel(root)
self.classChoose.title('Choose your Class')
self.classChoose.geometry('300x200+300+400')
self.labelChoose = Label(self.classChoose, text="Select the class you wish to be")
self.labelChoose.grid(row=0)
self.listClass = Listbox(self.classChoose, height=4)
self.listClass.grid(row=1)
self.listClass.insert(END,"Warrior")
self.confirm = Button(self.classChoose, text="Confirm", command=self.confirm)
self.confirm.grid(row=2)
def confirm(self):
global Name
global CharHpMax
global CharHpCurrent
global CharMpMax
global CharMpCurrent
global CharStr
global CharDef
global CharMAtt
global CharMDef
global Currentexp
global Requiredexp
fileClass = open('ClassData.txt','r')
for line in fileClass:
classInfo=line.split(',')
print "Name is:", Name
print "This class's stats are:",classInfo
className = classInfo[0]
CharHpMax = classInfo[1]
CharHpCurrent = classInfo[2]
CharMpMax = classInfo[3]
CharMpCurrent = classInfo[4]
CharStr = classInfo[5]
CharDef = classInfo[6]
CharMAtt = classInfo[7]
CharMDef = classInfo[8]
#sets the exp totals for the new game
Currentexp=0
Requiredexp=0
print "The name of the class is: ",className
print "The Character's max HP is: ",CharHpMax
print "The Character's Current Hp is: ",CharHpCurrent
print "The Character's max Mp is: ",CharMpMax
print "The Character's current Mp is: ",CharMpCurrent
print "The Character's Strength is: ",CharStr
print "The Character's Defence is: ", CharDef
print "The Character's Magic Power is: ", CharMAtt
print "The Character's Magic Resistance is: ", CharMDef
fileChar = open('Character Data\ '+Name+'.txt','a')
fileChar.writelines(className+','+CharHpMax+','+CharHpCurrent+','+CharMpMax+','+CharMpCurrent+','+CharStr+','+CharDef+','+CharMAtt+','+CharMDef+','+str(Currentexp)+','+str(Requiredexp))
mainGame()
self.classChoose.destroy()
class mainGame:
def __init__(self):
global Name
global CharHpMax
global CharHpCurrent
global CharMpMax
global CharMpCurrent
global CharStr
global CharDef
global CharMAtt
global CharMDef
self.gameMain = Toplevel(root)
self.gameMain.title('Role Playing Form V 1.0')
self.gameMain.geometry('400x500+350+400')
self.mainLabel = Label(self.gameMain, text="CHARACTER INFO:")
self.mainLabel.grid(row=0, column=0)
self.nameLabel = Label(self.gameMain, text="Name: "+Name)
self.nameLabel.grid(row=1, column=0)
self.HpLabel = Label(self.gameMain, text="HP: "+CharHpMax+"\ "+CharHpCurrent)
self.HpLabel.grid(row=2, column=0)
self.MpLabel = Label(self.gameMain, text="MP: "+CharMpMax+"\ "+CharMpCurrent)
self.MpLabel.grid(row=3, column=0)
self.StrLabel = Label(self.gameMain, text="Strength: "+CharStr)
self.StrLabel.grid(row=4, column=0)
self.DefLabel = Label(self.gameMain, text="Defence: "+CharDef)
self.DefLabel.grid(row=5, column=0)
self.MAttLabel = Label(self.gameMain, text="Magic Power: "+CharMAtt)
self.MAttLabel.grid(row=6, column=0)
self.MDefLabel = Label(self.gameMain, text="Magic Resistance: "+CharMDef)
self.MDefLabel.grid(row=7, column=0)
self.exitButton = Button(self.gameMain, text="Quit?", command=self.quitGame)
self.exitButton.grid(row=0, column=1)
def quitGame(self):
pass
root = Tk()
main=Main(root)
[/php]

And here's the format I'm hoping for for the text file:

Quote ...
Warrior,250,250,20,20,25,30,10,5
0,0
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Jan 31st, 2007
0

Re: Can't get grid to work!

You might want to start a new thread with the proper title. It will help you to get help!
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Feb 2nd, 2007
0

Re: Can't get grid to work!

This is going to be a suggestion that's painful to implement up front, but that will save you a lot of pain on the flipside.

Your classes currently correspond to actions that you want to take: creating a name, creating a class, launching the game. The actual object in question, though, is the character. That should, IMO, be your one class. Then, you can create methods of that class to create the name, get his class, etc.

It would work something like this:

Python Syntax (Toggle Plain Text)
  1. class Character(object):
  2.  
  3. def __init__(self, app): # app is the Tk() object you are linking to.
  4. self.window = app
  5.  
  6. def create_name(self):
  7. root = Toplevel(self.window)
  8. ... # create the modal dialog box for the name
  9. self.name = MyDialogReturnValue
  10.  
  11. def create_class(self):
  12. root = Toplevel(self.window)
  13. ... # create the modal dialog box for the class
  14. self.class = MyDialogReturnValue

Now, why might one want to re-write the code in this way?

(1) In the end, it will clean away some of the fuzziness. The fact that you have massive numbers of global variables is an indication that there is difficulty getting the classes to interface with each other. That difficulty will compound as your game code progresses.

(2) Creating a character class will help you to simplify some of the problems like reading and writing from files. For example, the Character class can have a __str__() method that returns his proper representation. Then the "write to file" method becomes a piece of cake!

Python Syntax (Toggle Plain Text)
  1.  
  2. def __str__(self):
  3. s = "name: "+self.name
  4. s += "\nclass: "+self.className
  5. s +="\nMax HP: "+self.CharHpMax
  6. #etc.
  7. return s
  8.  
  9. def load(self, filename):
  10. f = open(filename, 'r')
  11. #read in all characteristics as strings
  12. # You might be able to do this as a loop; I'm just being lazy
  13. line = f.readline()
  14. _, self.name = line.split(":")
  15. self.name = self.name.strip() # There ought to be a .= operator in Python!
  16. line = f.readline()
  17. _, self.className = line.split(":")
  18. self.className = self.className.strip()
  19. line = f.readline()
  20. _, self.CharHpMax = line.split(":")
  21. self.CharHpMax = int(self.CharHpMax)
  22. ... # etc.
  23.  
  24. f.close()
  25.  
  26. def save(self, filename):
  27. f = open(filename, 'w')
  28. f.write(str(self))
  29. f.close()

(3) There will come a day when you get the program working and want to allow more than one player to play. On that day, you will rejoice to have a character class that allows you to create *two or more* character objects, and *POOF*, you have a multi-player game. :cheesy:

I hope this is helpful rather than otherwise. Nevertheless, that's how I would refactor the code.

Regards,
Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Feb 5th, 2007
0

Re: Can't get grid to work!

So, what would doing that basically be creating the Character class, and moving all the def statements into the class, all calling each other as needed, with the code being cut/pasted into each def? like the character class will create the start menu too?
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Feb 5th, 2007
0

Re: Can't get grid to work!

I've turned the whole game into one class, and it works. I'm having trouble getting an image to appear on the fightZone form, it doesn't give error messages, but it is not displaying the image.
Here's the .zip
Attached Files
File Type: zip Role Playing Form.zip (7.1 KB, 15 views)
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Feb 5th, 2007
0

Re: Can't get grid to work!

Avoid all those globals, you are using Python! If the class variables are global within the class use the prefix 'self.' this way they are part of the class instance when you switch characters.

If your gif files are in the current folder's subfolder use:
grave=PhotoImage(file=r".\Images\TitleScreenGraves.gif")

When you call function in the class, also prefix with 'self.' since this function is method in the class.

Your present thread title is missleading and has nothing to do with grid! Like vegaseat and others recommend, start a new thread and call it "Can't get class to work!". I won't look here any further!
Last edited by bumsfeld; Feb 5th, 2007 at 4:17 pm. Reason: new thread
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Timing a Function (Python)
Next Thread in Python Forum Timeline: Merging python and java script





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC