The Gaming Hideout
The Gaming Hideout

Reputation: 584

Python 3.4 Assign variable with variable

This is a rather simple question;

I know that you can do:

foo = 1,
self.card1 = "This is card number %s." % (foo)

But I'm trying to assign that variable given by function to the name itself:

def card(foo):
  self.foo = foo
  self.usrCrdImg%self.foo = Image.open(self.crdStk[randint(1, 50)])
  self.usrCrdBg%self.foo = ImageTk.PhotoImage(usrCrdImg%self.foo)
  self.usrCrd%self.foo = tkinter.Label(root, image=usrCrdBg%self.foo)
  self.usrCrd%self.foo.image = self.usrCrdBg%self.foo

card(1)
#Should Execute like this
def card(1):
  self.usrCrdImg1 = Image.open(self.crdStk[randint(1, 50)])
  self.usrCrdBg1 = ImageTk.PhotoImage(usrCrdImg1)
  self.usrCrd1 = tkinter.Label(root, image=usrCrdBg1)
  self.usrCrd1.image = self.usrCrdBg1 

Once I get the answer to this question, I'll have another question to ask.

Here's my Code

import tkinter
import winsound
from tkinter import *
from PIL import Image, ImageTk
from random import randint, randrange

class CardGame(tkinter.Frame):

  def __init__(self, root):

    tkinter.Frame.__init__(self, root)
    
    #define variables for cards
    self.crdImg = []
    usrStk = None
    cpuStk = None
    #define card images
    i = 1
    while i < 57:
      i = i + 1
      self.crdImg.append('img/cards/%s.png' % (i - 1))

    usrStk = self.crdImg[54]
    cpuStk = self.crdImg[55]

  def debug(card):
    self.card = card
    self.usrCrdImg%self.card = Image.open(self.crdStk[randint(1, 50)])
    self.usrCrdBg%self.card = ImageTk.PhotoImage(self.usrCrdImg%self.card)
    self.usrCrd%self.card = tkinter.Label(root, image=self.usrCrdBg%self.card)
    self.usrCrd%self.card.image = self.usrCrdBg%i
  def card(self):
  
    ###
    self.usrStk1 = self.crdImg[54]
    self.cpuStk1 = self.crdImg[55]
    ##
    self.usrCrdImg1 = Image.open(self.usrStk1)
    self.usrCrdBg1 = ImageTk.PhotoImage(self.usrCrdImg1)
    self.usrCrd1 = tkinter.Label(root, image=self.usrCrdBg1)
    self.usrCrd1.image = self.usrCrdBg1
    ##
    self.cpuCrdImg1 = Image.open(self.cpuStk1)
    self.cpuCrdBg1 = ImageTk.PhotoImage(self.cpuCrdImg1)
    self.cpuCrd1 = tkinter.Label(root, image=self.cpuCrdBg1)
    self.cpuCrd1.image = self.cpuCrdBg1

Upvotes: 0

Views: 87

Answers (2)

Ben Morris
Ben Morris

Reputation: 626

If you Absolutely Must Create a variable, Eg. a Tkinter button, use something like:

vars = []
founditems = ['item', 'otheritem']
for i in founditems:
    vars.append(i)
for n in range(len(founditems)-1):
    exec('tkbutton' + vars[i] + '=' + 'Button(textvariable=tv' + vars[i] + ', command=lambda: buttoncommand(' + vars[i] + '))')

The basic format: exec(var + '=' + varvalue)

If you only need to define the variables name, just do exec('varname = ' + varvalue)

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 386352

What you are trying to do is just about the worst way to solve your problem. Generally speaking, you shouldn't ever try to generate variable names on the fly.

The simplest solution is to use a dictionary. Something like this:

def debug(card):
    self.card = card
    self.usrCrd[self.card]["Img"] = ... 
    self.usrCrd[self.card]["Bg"] = ...

Though, a better solution is to make a "card" be an instance of a class. Then, you can store these properties as attributes of the class:

class Card(object):
    def __init__(self, background, image):
        self.bg = background
        self.img = image
        ...

...
self.usrCrd = Card(...)
self.cpuCrd = Card(...)

Upvotes: 5

Related Questions