从checkbutton widge在Tkinter中分配全局变量

2024-10-01 09:18:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在做一个粗略的人口增长计算器类型的项目,只要人口持续增长而没有衰退或降低出生率,它就可以很好地工作。我一直在尝试让一个变量被认为是全局变量,这样就可以在计算总体的函数中读取它。在

但是,我似乎不知道在检查了checkbutton小部件时如何给变量赋值。下面是给你们看的代码:

'''shows human population growth rate through a certain year.
   It also has an option to include a list of prepared disasters
   that can lower the population.'''

from math import *
from Tkinter import *
import random

class App(Tk):
    def __init__ (self):
        Tk.__init__(self)
        '''Creates the set up for the Tkinter widgets with text on the left side, and the entry values on the right'''
        self.title("Population Growth")

        self.LblOutput = Label (self, foreground = "blue", font = "arial", text = "Enter the year you wish to see the world's population: ")
        self.LblOutput.grid(row = 1, column = 0)

        self.TxtInput = Entry(self)
        self.TxtInput.grid(row = 1, column = 1)

        self.LblOutput2 = Label (self, foreground = "blue", font = "arial", text = "Enter the desired Starting population: ")
        self.LblOutput2.grid(row = 2, column = 0)

        self.TxtInput2 = Entry(self)
        self.TxtInput2.grid(row = 2, column = 1)

        Label(self, foreground = "blue", font = "arial", text = "Turn on Natural Disasters?").grid(row = 3, column = 0)
        self.checkVar = IntVar()
        self.chkCheck = Checkbutton(self, text = "Natural Disasters", variable = self.checkVar, onvalue = 1, offvalue = 0, command = self.mortality)
        self.chkCheck.grid(row = 3, column = 1)

        self.StrtBtn = Button(self, foreground = "black", font = "arial", text = "Begin!", command = self.population_growth)
        self.StrtBtn.grid (row = 6, column = 0)

        self.TxtOutput = Label(self, foreground = "red", font = "gothica" , text = "The Population Is:").grid(row = 7, column = 0)
        self.LblTotal = Label(self)
        self.LblTotal.grid(row = 7, column = 1)

        self.mainloop()

    def mortality(self):
    '''checks to see if the checkmark is checked, if so assigns a random number to deathrate from the range, otherwise deathrate is 0'''
        if self.checkVar.get() == 1:
            deathrate = random.uniform(0.001, 0.00009903)
        elif self.checkVar.get() == 0:
            deathrate = 0
        global deathrate

    def population_growth(self):
        #Using the following equation P=P(subscript = 0)e^rt
        #P(subscript = 0)=initial population, 0 = time; for simplicity = p
        #e = euler's number [approximation]
        #rate_of_growth = rate of growth (found by subtracting crude death rate from crude birth rate
        #time_desired = time (years) that the population is calculated for
        time_desired = int(self.TxtInput.get())
        population_beginning = int(self.TxtInput2.get())
        e_mans_number = 2.71828182845904523536028747135266249775724709369995
        rate_of_growth = 0.01113 - int(deathrate)
        P = population_beginning * ((e_mans_number)**((rate_of_growth)*(time_desired)))
        self.LblTotal["text"] = " %d  people" % P 

def main():
    a = App()

if __name__ == "__main__":
    main()

如果我选中此框,它似乎可以正常工作,但一旦取消选中,该值不会更改。如果我用未选中的值启动它,它将给我一个错误,但是一旦我检查它,就没有错误。检查后,取消选择,没有错误,有什么帮助???在


Tags: ofthetextselfratetimecolumnlabel
3条回答

您不需要创建一个全局变量,当选中checkbutton时也不需要做任何事情。只需在需要时使用checkbutton变量:

def population_growth(self):
    ...
    deathrate = 0
    if self.checkVar.get() == 1:
        deathrate = random.uniform(0.001, 0.00009903)
    rate_of_growth = 0.01113 - deathrate

如果不想在函数中放入该逻辑,只需声明另一个实例变量。使用自失形公司名称:

^{pr2}$

为什么要把它变成一个全局变量? 全局变量在大多数情况下都是一个坏习惯。在

尝试以下操作:

class App(Tk):

def __init__ (self):
    Tk.__init__(self)
    '''Creates the set up for the Tkinter widgets with text on the left side, and the          entry values on the right'''
    self.title("Population Growth")
    self.deathRate = random.uniform(0.001, 0.00009903)
    [...]

现在,您可以通过以下方式访问您的死亡率和人口增长函数中的可变死角:

^{pr2}$

例如:

    def mortality(self):
        if self.checkVar.get() == 1:
            self.deathrate = random.uniform(0.001, 0.00009903)
        elif self.checkVar.get() == 0:
            self.deathrate = 0

    def population_growth(self):
        time_desired = int(self.TxtInput.get())
        population_beginning = int(self.TxtInput2.get())
        e_mans_number = 2.71828182845904523536028747135266249775724709369995
        rate_of_growth = 0.01113 - int(self.deathrate)
        P = population_beginning * ((e_mans_number)**((rate_of_growth)*(time_desired)))
        self.LblTotal["text"] = " %d  people" % P 

这称为实例变量。请参考http://www.python.org/doc/essays/ppt/acm-ws/sld051.htm或python教程(http://docs.python.org/2/tutorial/classes.html)。在

你好

在使用导入的类之前初始化deathrate

每当更改deathrate的值时,请执行以下操作:

global deathrate

更改值之前。你的其他代码运行良好

相关问题 更多 >