Переменная не будет обновляться при перемещении масштабной линейки на python tkinter - PullRequest
0 голосов
/ 11 декабря 2018

всякий раз, когда я запускаю свой код, переменная conta_rate не изменяется, когда пользователь взаимодействует со скользящей шкалой (от 0 до 100).Я попытался изменить IntVar на IntVar () и наоборот, но это не имеет никакого эффекта.

from tkinter import *
from epidemic import World


class MyEpidemicModel:
    def __init__(self, master):
        self.height = 500
        self.width = 500
        #self.infection_rate = 30
        self.infection_rate = IntVar()
        self.rate = Scale(orient='horizontal', from_=0, to=100, variable=self.infection_rate)
        self.rate.pack()
        self.infection_radius = (self.infection_rate/2)

        self.master = master
        master.title("An Epidemic Model")

        self.label = Label(master, text="This is our Epidemic Model")
        self.label.pack()

        self.canvas = Canvas(master, width = self.width, height = self.height)
        self.canvas.pack()

        self.inputCanvas = Canvas()

        self.start = Button(master, text="Start", command=self.start_epidemic)
        self.start.pack()

    def start_epidemic(self):
        self.canvas.delete('all')
        self.infection_radius = (self.infection_rate/2)
        self.this_world = World(self.height, self.width, 30, self.infection_rate)
        for person in self.this_world.get_people():
            x, y = person.get_location()
            if person.infection == True:
                self.canvas.create_oval(x,y, x+self.infection_radius, y+self.infection_radius, fill="red")
            else:
                self.canvas.create_oval(x, y, x+3, y+3, fill="yellow")
        self.update_epidemic()

И ошибка:

IndentationError: expected an indented block
PS C:\Users\Eva Morris> & python "c:/Users/Eva Morris/Documents/computing/epidemic/epidemicui.py"
Traceback (most recent call last):
  File "c:/Users/Eva Morris/Documents/computing/epidemic/epidemicui.py", line 76, in <module>
    my_gui = MyEpidemicModel(root)
  File "c:/Users/Eva Morris/Documents/computing/epidemic/epidemicui.py", line 13, in __init__
    self.infection_radius = (self.infection_rate/2)
TypeError: unsupported operand type(s) for /: 'IntVar' and 'int'
PS C:\Users\Eva Morris>

код

Сообщение об ошибке

1 Ответ

0 голосов
/ 12 декабря 2018

После настройки IntVar:

self.infection_rate = IntVar()
self.rate = Scale(orient='horizontal', from_=0, to=100, variable=self.infection_rate)

это не переменная Python :

    self.infection_radius = (self.infection_rate/2)
    self.this_world = World(self.height, self.width, 30, self.infection_rate)

, а Tkinterпеременная - вам нужно использовать .get() и .set() для доступа к ее значению:

    self.infection_radius = (self.infection_rate.get() / 2)
    self.this_world = World(self.height, self.width, 30, self.infection_rate.get())
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...