输入文本无法正确显示在画布上

2024-09-29 23:29:34 发布

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

我试图用Python中的tKinter库编写一个游戏。我做了三个功能,它们必须一起工作。两个函数用于显示名称输入并将其保存到变量中,而另一个函数用作游戏本身。列出的第一个函数是我用来将输入存储到变量中的函数。第二个函数用于名称输入。第三个功能是游戏本身

def input_ophalen():
    input = Naam1.get("1.0", "end-1c")
    print(input)
    input = Naam2.get("1.0", "end-1c")
    print(input)
    input = Naam3.get("1.0", "end-1c")
    print(input)

def create_kiezen():
    global Naam1
    global Naam2
    global Naam3

    kiezen = Tk()
    kiezen.geometry('600x600')
    kiezen.title('Namen invoeren!')
    kiezen.configure(background='darkgrey')
    Spelen = Button(kiezen, text='Begin!', bd='5', height='2', width='15',
                    command=lambda: [kiezen.destroy(), Spelmenu()])
    Terug = Button(kiezen, text='Terug', bd='5', height='2', width='15', command=lambda: [kiezen.destroy(), menu()])
    Spelen.pack(anchor='s', side='right')
    Terug.pack(anchor='s', side='left')
    kiezen.minsize(400, 400)
    kiezen.maxsize(400, 400)

    naamblok = Canvas(kiezen, width=300, height=200)
    naamblok.place(x=50, y=50)
    Opslaan=Button(naamblok, height=1, width=10, text='Opslaan', command=lambda: input_ophalen())
    Opslaan.place(relx=0.5, rely=0.7, anchor=CENTER)

    Naam1=Text(naamblok, width=15, height=1)
    Naam1.place(relx=0.5, rely=0.3, anchor=CENTER)
    Naam2= Text(naamblok, width=15, height=1)
    Naam2.place(relx=0.5, rely=0.4, anchor=CENTER)
    Naam3=Text(naamblok, width=15, height=1)
    Naam3.place(relx=0.5, rely=0.5, anchor=CENTER)

    mainloop()

def Spelmenu():
    global punt1
    global punt2
    global punt3

    spelmenu = Tk()
    spelmenu.geometry('400x400')
    spelmenu.title('Spelen maar!')
    spelmenu.configure(background='darkgrey')
    spelmenu.minsize(400, 400)
    spelmenu.maxsize(400, 400)
    volgendeB = Button(spelmenu, text='Volgende worp', bd='5', height='2', width='15')
    volgendeB.place(x=150, y=350)

    wBeurt = Canvas(spelmenu, width=300, height=60)
    wBeurt.place(x=50, y=40)
    wBeurt.create_text(150, 30, text="De beurt is aan " + str(Naam1))

    scores = Canvas(spelmenu, width=300, height=200)
    scores.place(x=50, y=130)
    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(156, 10, text="Je hebt " + fWorp1 + " en " + fWorp2 + " gegooit dus je hebt 2 punten gekregen!")
    else:
        punt1 = 5
        scores.create_text(156, 10, text="Je hebt " + fWorp1 + " en " + fWorp2 + " gegooit dus je hebt 5 punten gekregen!")

    mainloop()

代码的问题在于,每当你到达游戏的实际窗口时,它都会在顶部画布中显示以下内容:“De beurt是aan.!canvas.!text”。而不是“!canvas.!text”我希望有第一个玩家的名字。有人知道我该怎么解决这个问题吗


Tags: 函数text游戏inputplacewidthglobalanchor
1条回答
网友
1楼 · 发布于 2024-09-29 23:29:34

由于在Spelmenu()之前调用了keizen.destroy(),因此Naam1也将被销毁。因此,当在Spelmenu()内访问Naam1时,将引发异常

建议使用另一个全局StringVar{}来存储Naam1的内容,并在Spelmenu()内使用该变量:

def create_kiezen():
    global Naam1, Naam1_var
    ...
    Naam1_var = StringVar()
    Spelen = Button(kiezen, text='Begin!', bd='5', height='2', width='15',
                    command=lambda: [Naam1_var.set(Naam1.get("1.0", "end-1c")), kiezen.destroy(), Spelmenu()])
    ...

def Spelmenu():
    ...
    wBeurt.create_text(150, 30, text="De beurt is aan " + Naam1_var.get())
    ...

相关问题 更多 >

    热门问题