变量值不变吗? (蟒蛇)

时间:2019-06-07 22:36:43

标签: python tkinter

因此,我尝试将变量放入标签x ...中,以便当变量更改时,标签开始移动。

但是问题是,当我按下“下一步”按钮时,变量不会更改其值,而是保持在“ 0”

需要帮助:

import tkinter as tk

from docx import Document

HEIGHT = 500
WIDTH = 600

root = tk.Tk()
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

nextt=tk.IntVar()
nextt=0 #setup the nextt variable

label1 = tk.Label(text='example:')
label1.place(x=90+nextt,y=50) #put the variable here to affect position

def next2():
    nextt=300

button2 = tk.Button(text="Next", font=40, command=next2)
button2.place(x=230, y=440) 
#when pressing this button,variable nextt should change to 300, but it doesnt

root.mainloop()

我已经尝试过使用nextt.set = 300和/或nextt.get(),所有可能的组合...

1 个答案:

答案 0 :(得分:0)

函数lastScrollPosition中的

nextt是局部变量,您可以尝试将标签放在函数内部:

next2

如果以后要def next2(): label1.place(x = 390, y = 50) ,则将place用作全局变量(顺便说一句不是很好的解决方案):

nextt

仅更改def next2(): global nextt nextt = 300 # more code # ... label1.place(x = 90 + nextt, y = 50) 不会自动放置标签,您必须在代码中的其他位置再次放置。