简体   繁体   中英

Changing variable value in tkinter loop doesn't update

I'm working on a program and I ran into a problem I'm not sure how to fix.

I'm gonna try to give a simplified example below.

The purpose of the code is to read data from a device and display the stream live. However in the GUI You can select what stream of data You wish to display.

import tkinter
import datastream.py

dataselector = 3

def ReDraw(dataselector):
     if dataselector == 0:
          #draw a certain stream
     if dataselector == 1:
          #draw another stream
     #draw a bunch of other streams in other displays
     canvas.after(10,ReDraw,dataselector)

def SelectData(mouseevent):
    if event.clickedbutton == 0:
          #thatbuttonbecomesred
          dataselector = 0
    if event.clickedbutton == 1:
          #thatotherbuttonbecomesred
          dataselector = 1
    return dataselector

ReDraw(dataselector)
SelectData()

Sorry for the pseudo-code, but it's the simplest way to explain the problem.

The behavior I get is that everything draws and redraw correctly, the buttons do interact and become red BUT the ReDraw function only takes the original dataselector value and doesn't take the new one given by the SelectData function even if, testing with some prints, it indeed changes it.

It's like the ReDraw function takes the original value and store it secretly, ignoring any changes to that value!

I also tried using a global dataselector in the SelectData function instead, but it doesn't change anything.

Any suggestions how to fix this?

Just for clarifications, @PaulM. solved it in the comments above. Here is a version of the pseudo-code that would work correctly.

import tkinter
import datastream.py

dataselector = 3

def ReDraw():
     if dataselector == 0:
          #draw a certain stream
     if dataselector == 1:
          #draw another stream
     #draw a bunch of other streams in other displays
     canvas.after(10,ReDraw)

def SelectData(mouseevent):
    global dataselector
    if event.clickedbutton == 0:
          #thatbuttonbecomesred
          dataselector = 0
    if event.clickedbutton == 1:
          #thatotherbuttonbecomesred
          dataselector = 1

ReDraw()
SelectData()

Not sure why I didn't see it before, but thanks for the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM