如何在本地更新全局整数变量?

时间:2017-01-26 00:22:50

标签: python python-3.x variables tkinter scope

大家。

首先,我不确定这个问题是否得到了适当的赞扬,但我尽力而为。另外,请注意我刚开始学习Python,因为我想创建一个我想要使用的简单计算机程序。我想在这里提问,因为我遇到的问题似乎很容易让专业人士感到满意,但我似乎无法找到解决方案。

这是我写的代码......

from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk

class Application(Frame):
    def __init__(self, master):
        ttk.Frame.__init__(self, master)

        self.grid()
        self.create_widgets()
        self.FIDSIA_Amount()

    def create_widgets(self):
        self.label1 = Label(self, text='Type').grid(row=1, column=12, sticky=E)

        self.Type = ttk.Combobox(self, width=7, textvariable=varSymbol, state="readonly")
        self.Type['values'] = ('--------', 'A','B','C')
        self.Type.grid(row=1, column=13)
        self.Type.bind("<<ComboboxSelected>>", self.adjustmentFIDSIA_Planet1)

    def adjustmentFIDSIA_Planet1(self, event):
        content = self.Type.get()

        if content == 'A':
            varP1f = 0
            varP1ind = 12
            varP1d = 1
            varP1s = 0
            varP1inf = 0
            varP1app = -15
        elif content == 'B':
            varP1f = 0
            varP1ind = 12
            varP1d = 1
            varP1s = 0
            varP1inf = 0
            varP1app = -10
        elif content == 'C':
            varP1f = 0
            varP1ind = 12
            varP1d = 1
            varP1s = 0
            varP1inf = 0
            varP1app = -5
        elif content == '--------':
            varP1f = 0
            varP1ind = 0
            varP1d = 0
            varP1s = 0
            varP1inf = 0
            varP1app = 0

    def FIDSIA_Amount(self):
        amountf = Label(self, textvariable=varP1F)
        amountf.grid(row=1, column=1)
        amountind = Label(self, textvariable=varP1Ind)
        amountind.grid(row=1, column=3)
        amountd = Label(self, textvariable=varP1D)
        amountd.grid(row=1, column=5)
        amounts = Label(self, textvariable=varP1S)
        amounts.grid(row=1, column=7)
        amountinf = Label(self, textvariable=varP1Inf)
        amountinf.grid(row=1, column=9)
        amountapp = Label(self, textvariable=varP1App)
        amountapp.grid(row=1, column=11)

root = Tk()

varP1f = IntVar(root)
varP1ind = IntVar(root)
varP1d = IntVar(root)
varP1s = IntVar(root)
varP1inf = IntVar(root)
varP1app = IntVar(root)

varSymbol = StringVar(root, value='--------')
varP1F = StringVar(root)
varP1F.set(str(0+varP1f.get()))
varP1Ind = StringVar(root)
varP1Ind.set(str(0+varP1ind.get()))
varP1D = StringVar(root)
varP1D.set(str(0+varP1d.get()))
varP1S = StringVar(root)
varP1S.set(str(0+varP1s.get()))
varP1Inf = StringVar(root)
varP1Inf.set(str(0+varP1inf.get()))
varP1App = StringVar(root)
varP1App.set(str(0+varP1app.get()))

app = Application(root)

root.mainloop()

所以,基本上,我创建了一个包含6个数字的窗口,旁边有一个组合框下拉菜单,其中列出了4个项目(--------,A,B,C)。我想要做的是当在组合框中选择--------时,我希望6位数为0.但是,如果我在组合框中选择A,我希望通过添加在窗口中更新6位数因此,当我选择A时,它看起来像&#34; 0 12 1 0 0 -15&#34;而不是&#34; 0 0 0 0 0 0&#34;。使用当前代码,我似乎无法做到这一点。 PyCharm显示6个变量:varP1f等...有错误 - &gt;不使用局部变量值。

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

当您使用import {Version} from '@angular/core'; 个对象时,您应该使用tkinter.IntVar方法设置它们,而不是通过重新分配,因为set之类的内容会使x = tkinter.IntVar(); x = 3成为常规整数而不再是x

替换所有这样的行:

IntVar

有了这个:

if content == 'A':
    varP1f = 0
    varP1ind = 12
    varP1d = 1