在tkinter中将数字和文本打印到画布中

2024-10-03 11:22:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Python中的Tkinter模块将一个变量(一个数字)和文本一起打印到画布中。我似乎不知道该怎么做。这是我目前拥有的代码:

from tkinter import  *
from random import *

def Spelmenu():
    global punt1
    global punt2
    global punt3

    spelmenu = Tk()
    spelmenu.geometry('600x600')
    spelmenu.title('Spelen maar!')
    spelmenu.configure(background='darkgrey')
    spelmenu.minsize(600, 600)
    spelmenu.maxsize(600, 600)
    volgendeB = Button(spelmenu, text='Volgende beurt!', bd='5', height='2', width='15')
    volgendeB.place(x=240, y=550)

    Naam1="Jack"
    wBeurt = Canvas(spelmenu, width=500, height=60)
    wBeurt.place(x=50, y=70)
    wBeurt.create_text(250, 30, text=Naam1 + " is aan de beurt!")

    scores = Canvas(spelmenu, width=500, height=300)
    scores.place(x=50, y=160)
    worp1=randint(1,6)
    worp2=randint(1,6)
    fWorp1=str(worp1)
    fWorp2=str(worp2)

    if worp1 < worp2 or worp1 > worp2:
        punt1=2
        scores.create_text(text="Je hebt "+ fWorp1+ "en "+ fWorp2+ "gegooit dus je hebt 2 punten gekregen!")
    else:
        punt1=5
        scores.create_text(text="Je hebt "+ fWorp1+ "en "+ fWorp2+ "gegooit dus je hebt 5 punten gekregen!")

    mainloop()

Spelmenu()

这给我留下了这个错误

  TypeError: can only concatenate str (not "int") to str

有人对如何解决这个问题有什么想法吗


Tags: textcreateplacewidthglobalheightstrscores