0

everyone.

First of all, I am not sure if the question was praised properly but I tried as best as I could. Also, do note that I just started learning Python because I wanted to create a simple computer program I wanted to use. I wanted to ask question here because I encountered a problem that might seem easy to pros but I can't seem to figure out the solution.

Here is the code I wrote...

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()

So, basically, I created a window with 6 numbers in it and next to it, there is a combobox dropdown menu listing 4 items (--------, A, B, C). What I want to do is when -------- is selected in combobox, I want 6 digits to be 0. However, if I select A in combobox, I want the 6 digits to be updated in the window by adding each of the 6 variables on 0. So, when I choose A, it will look like "0 12 1 0 0 -15" instead of "0 0 0 0 0 0". With current code, I can't seem to do it. PyCharm shows 6 variables: varP1f, etc... have error -> local variable value is not used.

How can I fix this problem?

Jason M
  • 143
  • 1
  • 1
  • 7
  • always put in question FULL error message - there are other usefull information. – furas Jan 26 '17 at 00:37
  • did you mean `self.varP1f`? – Natecat Jan 26 '17 at 00:37
  • @juanpa.arrivillaga - That duplicate is often applicable, but in this case it isn't, as those are `IntVar` objects that should be mutated rather than integers to be reassigned. – TigerhawkT3 Jan 26 '17 at 00:40
  • @Natecat No I meant varP1f. I think. I am not exactly sure what self does. I was following youtube tutorials and it didn't explain well. – Jason M Jan 26 '17 at 00:47
  • @furas - When I ran, there was no error messages displayed on Shell. It's just that 6 digits didn't change to desired numbers and remained all 0s. But for future questions, I will post the errors. Thank you for the advice. – Jason M Jan 26 '17 at 00:49
  • @JasonM https://pythontips.com/2013/08/07/the-self-variable-in-python-explained/ – Natecat Jan 26 '17 at 01:09

1 Answers1

1

As you are using tkinter.IntVar objects, you should set them with their set method, not by reassignment, as something like x = tkinter.IntVar(); x = 3 will make that x a regular integer and no longer an IntVar.

Replace all lines like this:

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

With this:

if content == 'A':
    varP1f.set(0)
    varP1ind.set(12)
    varP1d.set(1)
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • Thank you for the response. I will remember to keep the variables more descriptive and unique. Also, I tried your solution but when I select A from combobox, Shell throws me this error "UnboundLocalError: local variable 'varP1ind' referenced before assignment". – Jason M Jan 26 '17 at 00:46
  • @JasonM in function you have to use `global varP1f`, etc. to inform function that you want to use external/global varaibles. Or put all this variables inside class with `self.` and you always with `self.` BTW: you can use `IntVar` also with `Label` so you don't need `StringVar`. – furas Jan 26 '17 at 01:23
  • @furas - I did some rearranging with TigerhawkT3's solution included and I finally made it happen. Also, I will take note of your comment. – Jason M Jan 26 '17 at 01:34
  • @JasonM BTW: http://pastebin.com/uAr8rPV2 – furas Jan 26 '17 at 01:42