0

I'm trying to find a way to draw text to the displayed card in such a way that it can be easily undone.

I've tried using a canvas for the cards, but I can't lift it to the top layer. The checkbox on the Item card is supposed to print the word 'Starter' on the card if it is checked, but it runs add_text even when not checked and doesn't add the text anyway. 2 days of google has turned up nothing.

# import everything from tkinter module
import tkinter as tk
import os
root = tk.Tk()
root.geometry('600x500')

fraItem = tk.Frame(root, height=500, width=600)
fraEnvi = tk.Frame(root, height=500, width=600)
fraMons = tk.Frame(root, height=500, width=600)
fraBoss = tk.Frame(root, height=500, width=600)
fraRace = tk.Frame(root, height=500, width=600)
fraClas = tk.Frame(root, height=500, width=600)

imgItem = tk.PhotoImage(file="ItemTemplate.gif")
imgEnvi = tk.PhotoImage(file="EnvironmentTemplate.gif")
imgMons = tk.PhotoImage(file="MonsterTemplate.gif")
imgBoss = tk.PhotoImage(file="BossTemplate.gif")
imgRace = tk.PhotoImage(file="RaceTemplate.gif")
imgClas = tk.PhotoImage(file="ClassTemplate.gif")


cardOverlay = tk.Canvas(root, height=400, width=300)
cardOverlay.place(x=250, y=20)


CARD_TYPES = ["Item", "Environment", "Monster", "Boss", "Race", "Class"]
currentType = tk.StringVar(root)
currentType.set("Item")

def make_card():
    print("Generating Card")

def clear_fields():
    print("Clear Fields")

def add_text(textStr, x, y):
    cardOverlay.create_text(x,y, text=textStr, fill="black", font=('Arial 15 bold'))
    print("put text on card")
    
def raise_buttons():
    lstTemplate.lift()
    butGenerate.lift()
    butClear.lift()
    
def card_changed(currentType):
    match currentType:
        case "Item":
            fraItem.tkraise()
            raise_buttons()
            print(currentType)
            cardOverlay.image = imgItem
        case "Environment":
            fraEnvi.tkraise()
            raise_buttons()
            print(currentType)
            cardOverlay.image = imgEnvi
        case "Monster":
            fraMons.tkraise()
            raise_buttons()
            print(currentType)
            cardOverlay.image = imgMons
        case "Boss":
            fraBoss.tkraise()
            raise_buttons()
            print(currentType)
            cardOverlay.image = imgBoss
        case "Race":
            fraRace.tkraise()
            raise_buttons()
            print(currentType)
            cardOverlay.image = imgRace
        case "Class":
            fraClas.tkraise()
            raise_buttons()
            print(currentType)
            cardOverlay.image = imgClas


#fraItem
lblItemName = tk.Label(fraItem, text="Card Name:").place(x=5, y=100)
txtItemName = tk.Entry(fraItem, text="enter card name").place(x=80, y=100)

lblItemStart = tk.Label(fraItem, text="Starting Item?:").place(x=5, y=125)
chkItemStart = tk.Checkbutton(fraItem, command=add_text('Starter', 200, 300)).place(x=80, y=125)

lblItemStats = tk.Label(fraItem, text="Stat Number:").place(x=5, y=150)
txtItemStats = tk.Entry(fraItem).place(x=80, y=150)

lblItemStatType = tk.Label(fraItem, text="Stat Type:").place(x=5, y=175)
txtStatType = tk.Entry(fraItem).place(x=80, y=175)

lblItemType = tk.Label(fraItem, text="Equip Type:").place(x=5, y=200)
txtItemType = tk.Entry(fraItem).place(x=80, y=200)

lblItemText = tk.Label(fraItem, text="Card Text:").place(x=5, y=225)
txtItemText = tk.Entry(fraItem).place(x=80, y=225)

lblItemPicture = tk.Label(fraItem, text="Complete Card:").place(x=250, y=5)
lblItemPicture = tk.Label(fraItem, height=400, image=imgItem).place(x=250, y=20)

#fraEnvi
lblEnviName = tk.Label(fraEnvi, text="Card Name:").place(x=5, y=100)
txtEnviName = tk.Entry(fraEnvi, text="enter room name").place(x=80, y=100)

lblEnviDeck = tk.Label(fraEnvi, text="Deck ID:").place(x=5, y=125)
txtEnviDeck = tk.Entry(fraEnvi, text="enter deck").place(x=80, y=125)

lblEnviType = tk.Label(fraEnvi, text="Card Type:").place(x=5, y=150)
txtEnviType = tk.Entry(fraEnvi, text="enter room type").place(x=80, y=150)

lblEnviText = tk.Label(fraEnvi, text="Card Text:").place(x=5, y=175)
txtEnviText = tk.Entry(fraEnvi, text="enter room text").place(x=80, y=175)

lblItemPicture = tk.Label(fraEnvi, text="Complete Card:").place(x=250, y=5)
lblItemPicture = tk.Label(fraEnvi, height=400, image=imgEnvi).place(x=250, y=20)

#fraMons
lblMonsName = tk.Label(fraMons, text="Name:").place(x=5, y=100)
txtMonsName = tk.Entry(fraMons, text="enter monster name").place(x=80, y=100)

lblMonsAtk = tk.Label(fraMons, text="Attack #:").place(x=5, y=125)
txtMonsAtk = tk.Entry(fraMons, text="enter attack").place(x=80, y=125)

lblMonsDef = tk.Label(fraMons, text="Defense #:").place(x=5, y=150)
txtMonsDef = tk.Entry(fraMons, text="enter defense").place(x=80, y=150)

lblMonsMag = tk.Label(fraMons, text="Magic #:").place(x=5, y=175)
txtMonsMag = tk.Entry(fraMons, text="enter magic").place(x=80, y=175)

lblMonsSpd = tk.Label(fraMons, text="Speed #:").place(x=5, y=200)
txtMonsSpd = tk.Entry(fraMons, text="enter speed").place(x=80, y=200)

lblMonsTarget = tk.Label(fraMons, text="Target:").place(x=5, y=225)
txtMonsTarget = tk.Entry(fraMons, text="enter target").place(x=80, y=225)

lblMonsType = tk.Label(fraMons, text="Type:").place(x=5, y=250)
txtMonsType = tk.Entry(fraMons, text="enter type").place(x=80, y=250)

lblMonsWound = tk.Label(fraMons, text="Wound Text:").place(x=5, y=275)
txtMonsWound = tk.Entry(fraMons, text="enter wound ability").place(x=80, y=275)

lblMonsText = tk.Label(fraMons, text="Main Text:").place(x=5, y=300)
txtMonsText = tk.Entry(fraMons, text="enter main text").place(x=80, y=300)

lblItemPicture = tk.Label(fraMons, text="Complete Card:").place(x=250, y=5)
lblItemPicture = tk.Label(fraMons, height=400, image=imgMons).place(x=250, y=20)

#fraBoss
lblBossName = tk.Label(fraBoss, text="Card Name:").place(x=5, y=100)
txtBossName = tk.Entry(fraBoss, text="enter boss name").place(x=80, y=100)

lblBossAtk = tk.Label(fraBoss, text="Attack #:").place(x=5, y=125)
txtBossAtk = tk.Entry(fraBoss, text="enter boss attack").place(x=80, y=125)

lblBossDef = tk.Label(fraBoss, text="Defense #:").place(x=5, y=150)
txtBossDef = tk.Entry(fraBoss, text="enter boss defense").place(x=80, y=150)

lblBossMag = tk.Label(fraBoss, text="Magic #:").place(x=5, y=175)
txtBossMag = tk.Entry(fraBoss, text="enter boss magic").place(x=80, y=175)

lblBossSpd = tk.Label(fraBoss, text="Speed #:").place(x=5, y=200)
txtBossSpd = tk.Entry(fraBoss, text="enter boss speed").place(x=80, y=200)

lblBossCombat = tk.Label(fraBoss, text="Combat:").place(x=5, y=225)
txtBossCombat = tk.Entry(fraBoss, text="enter combat effect").place(x=80, y=225)

lblBossEnvironment = tk.Label(fraBoss, text="Environment:").place(x=5, y=250)
txtBossEnvironment = tk.Entry(fraBoss, text="enter Environment effect").place(x=80, y=250)

lblBossMonster = tk.Label(fraBoss, text="Monster:").place(x=5, y=275)
txtBossMonster = tk.Entry(fraBoss, text="enter monster effect").place(x=80, y=275)

lblBossAct1 = tk.Label(fraBoss, text="Action 1:").place(x=5, y=300)
txtBossAct1 = tk.Entry(fraBoss, text="enter action 1").place(x=80, y=300)

lblBossAct2 = tk.Label(fraBoss, text="Action 2:").place(x=5, y=325)
txtBossAct2 = tk.Entry(fraBoss, text="enter action 2").place(x=80, y=325)

lblBossAct3 = tk.Label(fraBoss, text="Action 3:").place(x=5, y=350)
txtBossAct3 = tk.Entry(fraBoss, text="enter action 3").place(x=80, y=350)

lblBossAct4 = tk.Label(fraBoss, text="Action 4:").place(x=5, y=375)
txtBossAct4 = tk.Entry(fraBoss, text="enter action 4").place(x=80, y=375)

lblBossAct5 = tk.Label(fraBoss, text="Action 5:").place(x=5, y=400)
txtBossAct5 = tk.Entry(fraBoss, text="enter action 5").place(x=80, y=400)

lblBossPicture = tk.Label(fraBoss, text="Complete Card:").place(x=250, y=5)
lblBossPicture = tk.Label(fraBoss, height=400, image=imgBoss).place(x=250, y=20)

#fraRace
lblRaceName = tk.Label(fraRace, text="Card Name:").place(x=5, y=100)
txtRaceName = tk.Entry(fraRace, text="enter race name").place(x=80, y=100)

lblRaceAtk = tk.Label(fraRace, text="Attack #:").place(x=5, y=125)
txtRaceAtk = tk.Entry(fraRace, text="enter race attack").place(x=80, y=125)

lblRaceDef = tk.Label(fraRace, text="Defense #:").place(x=5, y=150)
txtRaceDef = tk.Entry(fraRace, text="enter Race defense").place(x=80, y=150)

lblRaceMag = tk.Label(fraRace, text="Magic #:").place(x=5, y=175)
txtRaceMag = tk.Entry(fraRace, text="enter Race magic").place(x=80, y=175)

lblRaceSpd = tk.Label(fraRace, text="Speed #:").place(x=5, y=200)
txtRaceSpd = tk.Entry(fraRace, text="enter Race speed").place(x=80, y=200)

lblRaceBonus = tk.Label(fraRace, text="Bonus:").place(x=5, y=225)
txtRaceBonus = tk.Entry(fraRace, text="enter Bonus").place(x=80, y=225)

lblRaceFlaw = tk.Label(fraRace, text="Flaw:").place(x=5, y=250)
txtRaceFlaw = tk.Entry(fraRace, text="enter Flaw").place(x=80, y=250)

lblRacePicture = tk.Label(fraRace, text="Complete Card:").place(x=250, y=5)
lblRacePicture = tk.Label(fraRace, height=400, image=imgRace).place(x=250, y=20)

#fraClas
lblClasName = tk.Label(fraClas, text="Card Name:").place(x=5, y=100)
txtClasName = tk.Entry(fraClas, text="enter class name").place(x=80, y=100)

lblClasAtk = tk.Label(fraClas, text="Attack #:").place(x=5, y=125)
txtClasAtk = tk.Entry(fraClas, text="enter Class attack").place(x=80, y=125)

lblClasDef = tk.Label(fraClas, text="Defense #:").place(x=5, y=150)
txtClasDef = tk.Entry(fraClas, text="enter Class defense").place(x=80, y=150)

lblClasMag = tk.Label(fraClas, text="Magic #:").place(x=5, y=175)
txtClasMag = tk.Entry(fraClas, text="enter Class magic").place(x=80, y=175)

lblClasSpd = tk.Label(fraClas, text="Speed #:").place(x=5, y=200)
txtClasSpd = tk.Entry(fraClas, text="enter Class speed").place(x=80, y=200)

lblClasAct = tk.Label(fraClas, text="Active:").place(x=5, y=225)
txtClasAct = tk.Entry(fraClas, text="enter Active").place(x=80, y=225)

lblClasPass = tk.Label(fraClas, text="Passive:").place(x=5, y=250)
txtClasPass = tk.Entry(fraClas, text="enter Passive").place(x=80, y=250)

lblClasPicture = tk.Label(fraClas, text="Complete Card:").place(x=250, y=5)
lblClasPicture = tk.Label(fraClas, height=400, image=imgClas).place(x=250, y=20)


#card choice dropdown and buttons
lstTemplate = tk.OptionMenu(root, currentType, *CARD_TYPES, command=card_changed)
lstTemplate.place(x=50, y=20)
butGenerate = tk.Button(root, text="Generate", command=make_card)
butGenerate.place(x=200, y=450)
butClear = tk.Button(root, text="Clear",fg="red", command=clear_fields)
butClear.place(x=350, y=450)




fraEnvi.place(x=0, y=0)
fraMons.place(x=0, y=0)
fraBoss.place(x=0, y=0)
fraRace.place(x=0, y=0)
fraClas.place(x=0, y=0)
fraItem.place(x=0, y=0)
card_changed("Item")
#window will not appear until mainloop is called
root.mainloop()


  • 2
    You've posted a _lot_ of code. Please try to reduce it down to a [mcve]. I don't think we need all of those labels and entries to reproduce this problem. – Bryan Oakley Dec 19 '22 at 15:35
  • `command=add_text('Starter', 200, 300)` Same issue as [here](https://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter). – 8349697 Dec 19 '22 at 15:55
  • That helped, but the text still won't show. – Shawn Guthrie Dec 19 '22 at 16:44
  • Your canvas is hidden under the frames. It occupies about half of the window, while the frame occupies the entire window. If you reduce the size of the frame (width), then the canvas becomes visible. You may need to rethink your design/layouts. – 8349697 Dec 19 '22 at 20:45
  • [Labels](https://tkdocs.com/widgets/label.html) can contain both an image and text. – 8349697 Dec 19 '22 at 21:08

0 Answers0