944,068 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1713
  • Python RSS
Dec 8th, 2006
0

Python GUI build: Logic Complications and Mistakes

Expand Post »
I have been working on a somewhat small Python GUI for a few weeks now primarily focusing on the GUI and not giving much in-depth thought to the logic of what is to be a simple card game. I assumed the logic to be in my head and in general it is but I am running into some problems I am having trouble solving.

Before I post some various code examples, I will tell you a bit of the trouble I am having: I have tried using both\ either dictionaries and lists to store card data; I see strengths and weaknesses in each for this program. I am not sure which would be the best at this point as I go back and forth trying different things and neither prove to be the winning answer. The trouble I believe lies in variable scope, specifically around the if\ elifs; there are many as there are 52 cards.

I have been working hard on this and I have tried many things but it seems when I think I solve one thing I have indeed tied myself up logically in another area locking myself into a corner it feels like. I think sometimes: "If I could just break out of the scope and access the nested variable from within." Yes, there is a way, I assume, but either I cannot recall it or do not know of it.

The game will be a basic "21" game. I have researched code and tried other's work but I felt initially I could make something better and do it all on my own. I need some personal assistance from someone knowledgable with this matter as I feel like I am going around and around.

I have learned some much though while doing this code and that is what really counts.

The actual problem reads like this: I wish to print 5 cards to the GUI screen. The Tkinter GUI work I need no help with--it is the logic at this point. I am missing something. I can print the first 5 cards to the screen easy enough but the code is hard-coded as it were and this is of no help-- I need it pseudo-random from a list or dictionary. OR, I run a random list or GUI which works fine but I am getting error messages such as:

Python Syntax (Toggle Plain Text)
  1. C:/Python24/pythonw.exe -u "C:/Documents and Settings/RockStar/Desktop/Python/drpython-161/phillies.py"
  2. [50, 28, 26, 14, 38, 10, 18, 31, 37, 19, 12, 27, 46, 41, 30, 17, 23, 51, 43, 45, 15, 35, 24, 25, 52, 39, 33, 2, 6, 13, 20, 11, 16, 8, 4, 40, 34, 29, 22, 42, 1, 9, 49, 47, 36, 7, 5, 3, 48, 32, 44, 21]
  3. 50
  4. 50
  5. x-x-x-x-x-x
  6. 50
  7. pyimage1
  8. Traceback (most recent call last):
  9. File "C:/Documents and Settings/RockStar/Desktop/Python/drpython-161/phillies.py", line 94, in ?
  10. photo1 = PhotoImage(file=image1)
  11. File "C:\Python24\lib\lib-tk\Tkinter.py", line 3203, in __init__
  12. Image.__init__(self, 'photo', name, cnf, master, **kw)
  13. File "C:\Python24\lib\lib-tk\Tkinter.py", line 3159, in __init__
  14. self.tk.call(('image', 'create', imgtype, name,) + options)
  15. _tkinter.TclError: couldn't open "50": no such file or directory
  16.  

This is the primary error I am contending with. I am so close to getting this to run beautifully I think. I have been doing hours of testing and editing, but keep coming back with the same or similar problems. Even when it runs it is just 5, set card defs printed to screen.

Any help, thoughts, directions, or hints will be greatly appreciated.

sharky_machine

GUI runs and renders but only from predefined
Python Syntax (Toggle Plain Text)
  1. from Tkinter import *
  2. import random
  3. import time
  4. root = Tk()
  5. #root.wm_iconbitmap('vvvv.ico')
  6.  
  7. root.title("[21]")
  8.  
  9. #HIT button output ?
  10. def show_image2():
  11. canvas1.create_image(50, 320, image=photo2)
  12.  
  13.  
  14. # make a list into random order
  15. cards = range(52)
  16. random.shuffle(cards) # order is random now
  17. pod = cards [1] # this takes card, puts in var
  18. print pod # test print pod
  19.  
  20.  
  21.  
  22. #==================== = = IF for keying int to card GIFs = = #
  23.  
  24. if pod == 0:
  25. print "loaded"
  26. var = "ewokJ.GIF"
  27. elif pod == 1:
  28. print "loaded"
  29. var = "droidsJ.GIF"
  30. elif pod == 2:
  31. print "loaded"
  32.  
  33.  
  34.  
  35. cards.remove(pod) ##--- remove card from list
  36. print cards
  37.  
  38. ##----- BUILD checkpoint -----##
  39. #Prep and create card images here
  40. image0 = "base_sith3.GIF"
  41. photo0 = PhotoImage(file=image0)
  42. image1 = "impbotJ.GIF"
  43. photo1 = PhotoImage(file=image1)
  44. image2 = "trooperJ.GIF"
  45. photo2 = PhotoImage(file=image2)
  46. image3 = "droidsJ.GIF"
  47. photo3 = PhotoImage(file=image3)
  48. image4 = "ewokJ.GIF"
  49. photo4 = PhotoImage(file=image4)
  50. image5 = "chewieQ.GIF"
  51. photo5 = PhotoImage(file=image5)
  52. image6 = "grievousQ.GIF"
  53. photo6 = PhotoImage(file=image6)
  54. image7 = "fettQ.GIF"
  55. photo7 = PhotoImage(file=image7)
  56. image8 = "hanQ.GIF"
  57. photo8 = PhotoImage(file=image8)
  58. image9 = "darthK.GIF"
  59. photo9 = PhotoImage(file=image9)
  60. image10 = "empK.GIF"
  61. photo10 = PhotoImage(file=image10)
  62. image11 = "leiaK.GIF"
  63. photo11 = PhotoImage(file=image11)
  64. image12 = "lukeK.GIF"
  65. photo12 = PhotoImage(file=image12)
  66. image13 = "darthA.GIF"
  67. photo13 = PhotoImage(file=image13)
  68. image14 = "empA.GIF"
  69. photo14 = PhotoImage(file=image14)
  70. image15 = "leiaA.GIF"
  71. photo15 = PhotoImage(file=image15)
  72. image16 = "lukeA.GIF"
  73. photo16 = PhotoImage(file=image16)
  74. image17 = "H2.GIF"
  75. photo17 = PhotoImage(file=image17)
  76. image18 = "H3.GIF"
  77. photo18 = PhotoImage(file=image18)
  78. image19 = "H4.GIF"
  79. photo19 = PhotoImage(file=image19)
  80. image20 = "H5.GIF"
  81. photo20 = PhotoImage(file=image20)
  82. image21 = "H6.GIF"
  83. photo21 = PhotoImage(file=image21)
  84. image22 = "H7.GIF"
  85. photo22 = PhotoImage(file=image22)
  86. image23 = "H8.GIF"
  87. photo23 = PhotoImage(file=image23)
  88. image24 = "H9.GIF"
  89. photo24 = PhotoImage(file=image24)
  90. image25 = "HT.GIF"
  91. photo25 = PhotoImage(file=image25)
  92. image26 = "C2.GIF"
  93. photo26 = PhotoImage(file=image26)
  94. image27 = "C3.GIF"
  95. photo27 = PhotoImage(file=image27)
  96. image28 = "C4.GIF"
  97. photo28 = PhotoImage(file=image28)
  98. image29 = "C5.GIF"
  99. photo29 = PhotoImage(file=image29)
  100. image30 = "C6.GIF"
  101. photo30 = PhotoImage(file=image30)
  102. image31 = "C7.GIF"
  103. photo31 = PhotoImage(file=image31)
  104. image32 = "C8.GIF"
  105. photo32 = PhotoImage(file=image32)
  106. image33 = "C9.GIF"
  107. photo33 = PhotoImage(file=image33)
  108. image34 = "CT.GIF"
  109. photo34 = PhotoImage(file=image34)
  110. image35 = "S2.GIF"
  111. photo35 = PhotoImage(file=image35)
  112. image36 = "S3.GIF"
  113. photo36 = PhotoImage(file=image36)
  114. image37 = "S4.GIF"
  115. photo37 = PhotoImage(file=image37)
  116. image38 = "S5.GIF"
  117. photo38 = PhotoImage(file=image38)
  118. image39 = "S6.GIF"
  119. photo39 = PhotoImage(file=image39)
  120. image40 = "S7.GIF"
  121. photo40 = PhotoImage(file=image40)
  122. image41 = "S8.GIF"
  123. photo41 = PhotoImage(file=image41)
  124. image42 = "S9.GIF"
  125. photo42 = PhotoImage(file=image42)
  126. image43 = "ST.GIF"
  127. photo43 = PhotoImage(file=image43)
  128. image44 = "D2.GIF"
  129. photo44 = PhotoImage(file=image44)
  130. image45 = "D3.GIF"
  131. photo45 = PhotoImage(file=image45)
  132. image46 = "D4.GIF"
  133. photo46 = PhotoImage(file=image46)
  134. image47 = "D5.GIF"
  135. photo47 = PhotoImage(file=image47)
  136. image48 = "D6.GIF"
  137. photo48 = PhotoImage(file=image48)
  138. image49 = "D7.GIF"
  139. photo49 = PhotoImage(file=image49)
  140. image50 = "D8.GIF"
  141. photo50 = PhotoImage(file=image50)
  142. image51 = "D9.GIF"
  143. photo51 = PhotoImage(file=image51)
  144. image52 = "DT.GIF"
  145. photo52 = PhotoImage(file=image52)
  146. ##----BUILD checkpoint ----##
  147.  
  148.  
  149. #Canvas created here \/
  150. # make canvas the size of image1/photo1
  151. width1 = photo0.width()
  152. height1 = photo0.height()
  153. canvas1 = Canvas(width=width1, height=height1)
  154. canvas1.pack()
  155. #display photo1, x, y is center
  156. x = (width1)/2.0
  157. y = (height1)/2.0
  158. canvas1.create_image(x, y, image=photo0)
  159.  
  160. ###------Paint cards to screen
  161. time.sleep(3)
  162. root.update()
  163. canvas1.create_image (50,325, image=photo1 )
  164. time.sleep(1)
  165. root.update()
  166. canvas1.create_image (50,175, image=photo2 )
  167. time.sleep(2)
  168. root.update()
  169. canvas1.create_image (125,325, image=photo3 )
  170. time.sleep(2)
  171. root.update()
  172. canvas1.create_image (125,175, image=photo4)
  173.  
  174. #---TEST LOAD from int RNG =========== WORKS
  175. root.update()
  176. canvas1.create_image (200,325, image=photo5)
  177. root.update()
  178. canvas1.create_image (200,325, image=photo6)
  179. root.update()
  180. canvas1.create_image (200,325, image=photo7)
  181.  
  182. #---TEST LOAD from int RNG
  183.  
  184.  
  185. image100 = PhotoImage(file='grey1.gif') # PIC for button---note 100!
  186. # create a button to display card(s)
  187. btn1 = Button(root, text="HIT", image=image100,command=show_image2)# PIC for button---note 100!
  188. btn1. pack()
  189.  
  190.  
  191. root.mainloop()


Will not run
Python Syntax (Toggle Plain Text)
  1. canvas1.pack()
  2. # display photo1, x, y is center
  3. x = (width1)/2.0
  4. y = (height1)/2.0
  5. canvas1.create_image(x, y, image=photo0)
  6. print photo0
  7. ## ----- /\ FRAME BUILD /\ ----- ##
  8.  
  9. # setting "photo" to load at bottom for GUI display
  10. image1 = var1
  11. photo1 = PhotoImage(file=image1)
  12. print var1
  13. image2 = var2
  14. photo2 = PhotoImage(file=image2)
  15. image3 = var3
  16. photo3 = PhotoImage(file=image3)
  17. image4 = var4
  18. photo4 = PhotoImage(file=image4)
  19. image5 = var5
  20. photo5 = PhotoImage(file=image5)
  21. #============ /\ TEST first 5 /\
  22.  
  23.  
  24. image6 = var6
  25. photo6 = PhotoImage(file=image6)
  26. image7 = var7
  27. photo7 = PhotoImage(file=image7)
  28. image8 = var8
  29. photo8 = PhotoImage(file=image8)
  30. image9 = var9
  31. photo9 = PhotoImage(file=image9)
  32. image10 = var10
  33. photo10 = PhotoImage(file=image10)
  34. image11 = var11
  35. photo11 = PhotoImage(file=image11)
  36. image12 = var12
  37. photo12 = PhotoImage(file=image12)
  38. image13 = var13
  39. photo13 = PhotoImage(file=image13)
  40. image14 = var14
  41. photo14 = PhotoImage(file=image14)
  42. image15 = var15
  43. photo15 = PhotoImage(file=image15)
  44. image16 = var16
  45. photo16 = PhotoImage(file=image16)
  46. image17 = var17
  47. photo17 = PhotoImage(file=image17)
  48. image18 = var18
  49. photo18 = PhotoImage(file=image18)
  50. image19 = var19
  51. photo19 = PhotoImage(file=image19)
  52. image20 = var20
  53. photo20 = PhotoImage(file=image20)
  54. image21 = var21
  55. photo21 = PhotoImage(file=image21)
  56. image22 = var22
  57. photo22 = PhotoImage(file=image22)
  58. image23 = var23
  59. photo23 = PhotoImage(file=image23)
  60. image24 = var24
  61. photo24 = PhotoImage(file=image24)
  62. image25 = var25
  63. photo25 = PhotoImage(file=image25)
  64. image26 = var26
  65. photo26 = PhotoImage(file=image26)
  66. image27 = var27
  67. photo27 = PhotoImage(file=image27)
  68. image28 = var28
  69. photo28 = PhotoImage(file=image28)
  70. image29 = var29
  71. photo29 = PhotoImage(file=image29)
  72. image30 = var30
  73. photo30 = PhotoImage(file=image30)
  74. image31 = var31
  75. photo31 = PhotoImage(file=image31)
  76. image32 = var32
  77. photo32 = PhotoImage(file=image32)
  78. image33 = var33
  79. photo33 = PhotoImage(file=image33)
  80. image34 = var34
  81. photo34 = PhotoImage(file=image34)
  82. image35 = var35
  83. photo35 = PhotoImage(file=image35)
  84. image36 = var36
  85. photo36 = PhotoImage(file=image36)
  86. image37 = var37
  87. photo37 = PhotoImage(file=image37)
  88. image38 = var38
  89. photo38 = PhotoImage(file=image38)
  90. image39 = var39
  91. photo39 = PhotoImage(file=image39)
  92. image40 = var40
  93. photo40 = PhotoImage(file=image40)
  94. image41 = var41
  95. photo41 = PhotoImage(file=image41)
  96. image42 = var42
  97. photo42 = PhotoImage(file=image42)
  98. image43 = var43
  99. photo43 = PhotoImage(file=image43)
  100. image44 = var44
  101. photo44 = PhotoImage(file=image44)
  102. image45 = var45
  103. photo45 = PhotoImage(file=image45)
  104. image46 = var46
  105. photo46 = PhotoImage(file=image46)
  106. image47 = var47
  107. photo47 = PhotoImage(file=image47)
  108. image48 = var48
  109. photo48 = PhotoImage(file=image48)
  110. image49 = var49
  111. photo49 = PhotoImage(file=image49)
  112. image50 = var50
  113. photo50 = PhotoImage(file=image50)
  114. image51 = var51
  115. photo51 = PhotoImage(file=image51)
  116. image52 = var52
  117. photo52 = PhotoImage(file=image52)
  118.  
  119. root.mainloop()
Similar Threads
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Dec 8th, 2006
1

Re: Python GUI build: Logic Complications and Mistakes

Looks like yo want to open a file called "50" even though the list is all integers.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Dec 9th, 2006
0

Re: Python GUI build: Logic Complications and Mistakes

Thank-you so much for your help and pointing out this simple solution to me, a glaringly obvious mistake I could not see. Sometimes it takes another person to look at something you've been staring at for hours\ days to see what you are missing.

Thanks again!

sharky_machine
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Dec 9th, 2006
0

Re: Python GUI build: Logic Complications and Mistakes

These are just general suggestions, but I hope they will be helpful rather than leading you down a wrong path.

(1) Creating a card class will solve a lot of problems for you. You can do something like this:

Python Syntax (Toggle Plain Text)
  1.  
  2. class Card(object):
  3.  
  4. images = {0:"ace_spades.bmp", 1:"two_spades.bmp"} # etc.
  5. strings = {0:"AS", 1: "2S"} # etc.
  6.  
  7. def __init__(self, value=None):
  8. if not value or value < 0 or value > 51:
  9. self.value = random.randint[0,51]
  10. else:
  11. self.value = value
  12.  
  13. def __str__(self):
  14. return Card.strings[self.value]
  15.  
  16. def display(self,canvas):
  17. # display self on the canvas using Card.images[self.value]
the Card.images value is now a class-wide constant, which I think takes care of the problem with variable scope.

(2) You can then create a Card.used_values list to keep track of already-dealt cards in the hand. You could perhaps modify the __init__ to look up the used_values and not deal any of those.

Hope it helps!

Jeff
Last edited by jrcagle; Dec 9th, 2006 at 1:04 pm. Reason: additional thoughts
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006

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: Why use a Python class?
Next Thread in Python Forum Timeline: Python Indentation





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


Follow us on Twitter


© 2011 DaniWeb® LLC