如何在tkinter中实际显示一个变量?

时间:2018-06-09 19:22:10

标签: python python-3.x

所以基本上我正在练习使用Tkinter,我打算根据动漫制作一个点击游戏。我已经研究了负载,但是,无论我怎样尝试文本说我赢了多少RC单元并没有显示任何东西。请保持简单,因为我还不熟悉编码。

from tkinter import *
rcc=1

x=1


#click function
def click():
global rcc
rcc=rcc+1
return rcc

while x==1:
 rccc=str(rcc)
 window=Tk()
 window.title("my click game boi")
 rc=StringVar()
 rc.set=(rccc)
 window.configure(background="black")
 ##### my photo
photo1=PhotoImage(file="kanekipic.gif")
Label (window, image=photo1, bg='black') .grid(row=0, column=0, sticky=E)
Button(window, text="eat", width=3, command=click) .grid(row=3, column=0, 
sticky=W)


Label1=Label(window, textvariable=rc, bg="red", fg="white") .grid(row=1, column=0, sticky=S)

print (rcc)


window.mainloop()

1 个答案:

答案 0 :(得分:0)

最好的解决方案(从某种意义上说,它保留了预期的目标并且保持简单"),我想出的是:

#!/usr/bin/env python3

from tkinter import *

rcc = 1

def click():
    global rcc
    rcc += 1
    rc.set(rcc)
    print('rcc was increased to {}'.format(rcc))


if __name__ == '__main__':
    window = Tk()
    window.title("my click game boi")
    window.configure(background='black')

    rc = IntVar()

    photo1 = PhotoImage(file="kanekipic.gif")
    Label(window, image=photo1, bg='black').grid(row=0, column=0, sticky=E)
    Button(window, text="eat", width=3, command=click).grid(row=3, column=0, sticky=W)
    Label(window, textvariable=rc, bg="red", fg="white").grid(row=1, column=0, sticky=S)

    window.mainloop()

每当您点击eat按钮时,计数器就会增加。