Python Tkinter文本输出

时间:2015-05-04 15:23:29

标签: python tkinter

我正在与Tkinter进行一场真理或大胆的比赛。我收到这个错误:

 UnboundLocalError: local variable 'text' referenced before assignment

执行此代码时:

from tkinter import *
import random

tk = Tk()
tk.title("Truth or Dare")
tk.resizable(0, 0)
canvas = Canvas(tk, width=500,height=350)
canvas.create_text(250, 20, text="Truth Or Dare", fill='red', font=('Helvetica', 20))
text = canvas.create_text(250, 200, text='Test!')

def spm():
    canvas.delete(text)
    spillere = ['Niels','Astrid']
    p1 = random.choice(spillere)
    p2 = random.choice(spillere)
    x1 = random.randint(0,3)
    s1 = ['skal spise kage','sig huj ud af vinduet'] # En udfordring til en spiller
    s2 = ['kan du lide kage?','har du nogensinde danset step?'] # Et spørgsmål til en spiller
    s3 = ['skal sige hej til','skal sige huj til','skal spise kage med'] # Noget med en anden spiller
    s4 = ['vil du spise kage med','vil du spise kage med'] # Et spørgsmål om en anden spiller
    while True:
        if p1 == p2:
            p2 = random.choice(spillere)
        else:
            break
    if x1 == 0:
        text = canvas.create_text(250, 200, text='%s %s' % (p1, random.choice(s1)), font=('Helvetica', 15))
    elif x1 == 1:
        text = canvas.create_text(250, 200, text='%s %s' % (p1, random.choice(s2)), font=('Helvetica', 15))
    elif x1 == 2:
        text = canvas.create_text(250, 200, text='%s %s %s' % (p1, random.choice(s3), p2), font=('Helvetica', 15))
    elif x1 == 3:
        text = canvas.create_text(250, 200, text='%s %s %s?' % (p1, random.choice(s4), p2), font=('Helvetica', 15))


btn = Button(tk, text="Klik!", command=spm, width=50,height=5,font=('Helvetica' , 15))
btn.pack()
canvas.pack()

我希望在按下按钮时在我的画布上显示字符串,并在再次按下按钮时显示一个新字符串而不在同一位置有两个字符串。

谢谢。

3 个答案:

答案 0 :(得分:0)

text声明为全局值。否则,解释程序会认为text是本地的,并且在您canvas.delete(text)套件中指定if之前在def spm(): global text #...etc 中引用它时会感到困惑。

{{1}}

答案 1 :(得分:0)

spm函数中的

文本是本地作用域的,与函数之前声明的全局文本变量不同

我总是觉得让gui环境更好地工作更好,我将文本变量分配更改为id,因为canvas.create_text返回被操作的画布的id,而不是任何实际的文本值。你可以通过利用这个课程找到它,它会起作用。

from Tkinter import *
import random

tk = Tk()
tk.title("Truth or Dare")
tk.resizable(0, 0)
canvas = Canvas(tk, width=500,height=350)
canvas.create_text(250, 20, text="Truth Or Dare", fill='red', font=    ('Helvetica', 20))
class Window(object):
    def __init__(self):
        self.id = canvas.create_text(250, 200, text='Test!')

    def spm(self):
        canvas.delete(self.id)
        spillere = ['Niels','Astrid']
        p1 = random.choice(spillere)
        p2 = random.choice(spillere)
        x1 = random.randint(0,3)
        s1 = ['skal spise kage','sig huj ud af vinduet'] # En    udfordring til en spiller
        s2 = ['kan du lide kage?','har du nogensinde danset step?']
        s3 = ['skal sige hej til','skal sige huj til','skal spise kage me d'] # Noget med en anden spiller
        s4 = ['vil du spise kage med','vil du spise kage med']
        while True:
            if p1 == p2:
                p2 = random.choice(spillere)
            else:
                break
        if x1 == 0:
            self.id = canvas.create_text(250, 200, text='%s %s' % (p1, random.choice(s1)), font=('Helvetica', 15))
        elif x1 == 1:
            self.id = canvas.create_text(250, 200, text='%s %s' % (p1,random.choice(s2)), font=('Helvetica', 15))
        elif x1 == 2:
            self.id = canvas.create_text(250, 200, text='%s %s %s' % (p1, random.choice(s3), p2), font=('Helvetica', 15))
        elif x1 == 3:
            self.id = canvas.create_text(250, 200, text='%s %s %s?' % (p1, random.choice(s4), p2), font=('Helvetica', 15))

window = Window()
btn = Button(tk, text="Klik!", command=window.spm,     width=50,height=5,font=('Helvetica' , 15))
btn.pack()
canvas.pack()
tk.mainloop()

答案 2 :(得分:-1)

不要将文本用作变量名称:

text = canvas.create_text(250, 200, text='Test!')

应该成为(例如):

mytext = canvas.create_text(250, 200, text='Test!')