-2
from tkinter import *

def Game(result):
    InputScreen = Tk()
    InputScreen.configure(background = "light blue")
    InputScreen.geometry("800x400+100+50")
    InputScreen.title("Input Screen")

    gold_e = StringVar()

    textentry2 = Entry(InputScreen,
                textvariable = gold_e, width = 5).place(x = 325, y = 80)


    button3 = Button(InputScreen,
                     command = lambda: (Update(gold_e)),
                     text = "Update",
                     fg = "black",
                     bg = "red").place(x = 300, y = 300)

    def Update(values):
        print("qqw"+str(values.get())+"wer")
        gold_v = values.get()
        print(gold_v)

        Label19 = Label(InputScreen, text = gold_v,
                        fg = "black",
                        bg = "light blue").place(x = 390,y = 80)

Game(0)

This Code works absolutly fine: However when it's surrounded by the following code, it just always returns '' (Blank) and I have no idea why.

import sys
from tkinter import *

def Game(result):
    InputScreen = Tk()
    InputScreen.configure(background = "light blue")
    InputScreen.geometry("800x400+100+50")
    InputScreen.title("Input Screen")

    goldpps = "0.5"
    silverpps = "0.8"
    copperpps = "1.2"
    oilpps = "3.4"
    wheatpps = "0.6"
    dieselpps = "0.8"
    cocopps = "3.0"

    Label0 = Label(InputScreen, text = "£" + str(result),
                   fg = "black",
                   bg = "light blue").place(x = 100,y = 20)

    Label1 = Label(InputScreen, text = "Commodities",
                   fg = "black",
                   bg = "light blue").place(x = 100,y = 50)

    Label2 = Label(InputScreen, text = "Gold",
                   fg = "black",
                   bg = "light blue").place(x = 125,y = 80)

    Label3 = Label(InputScreen, text = "Silver",
                       fg = "black",
                       bg = "light blue").place(x = 124,y = 110)

    Label4 = Label(InputScreen, text = "Copper",
                   fg = "black",
                   bg = "light blue").place(x = 119,y = 140)

    Label5 = Label(InputScreen, text = "Oil",
                   fg = "black",
                   bg = "light blue").place(x = 130,y = 170)

    Label6 = Label(InputScreen, text = "Wheat",
                   fg = "black",
                   bg = "light blue").place(x = 120,y = 200)

    Label7 = Label(InputScreen, text = "Diesel",
                   fg = "black",
                   bg = "light blue").place(x = 121,y = 230)

    Label8 = Label(InputScreen, text = "Coco",
                   fg = "black",
                   bg = "light blue").place(x = 122,y = 260)

    Label9 = Label(InputScreen, text = "Price per Share",
                   fg = "black",
                   bg = "light blue").place(x = 200,y = 50)

    Label10 = Label(InputScreen, text = "Quantity",
                   fg = "black",
                   bg = "light blue").place(x = 315,y = 50)

    Label11 = Label(InputScreen, text = "Cost",
                   fg = "black",
                   bg = "light blue").place(x = 390,y = 50)

    Label12 = Label(InputScreen, text = goldpps,
                   fg = "black",
                   bg = "light blue").place(x = 227,y = 80)

    Label13 = Label(InputScreen, text = silverpps,
                   fg = "black",
                   bg = "light blue").place(x = 227,y = 110)

    Label14 = Label(InputScreen, text = copperpps,
                   fg = "black",
                   bg = "light blue").place(x = 227,y = 140)

    Label15 = Label(InputScreen, text = oilpps,
                   fg = "black",
                   bg = "light blue").place(x = 227,y = 170)

    Label16 = Label(InputScreen, text = wheatpps,
                   fg = "black",
                   bg = "light blue").place(x = 227,y = 200)

    Label17 = Label(InputScreen, text = dieselpps,
                   fg = "black",
                   bg = "light blue").place(x = 227,y = 230)

    Label18 = Label(InputScreen, text = cocopps,
                   fg = "black",
                   bg = "light blue").place(x = 227,y = 260)


    silver_e = IntVar()
    copper_e = IntVar()
    oil_e = IntVar()
    wheat_e = IntVar()
    diesel_e = IntVar()
    coco_e = IntVar()
    gold_e = StringVar()

    textentry2 = Entry(InputScreen,
                textvariable = gold_e, width = 5).place(x = 325, y = 80)

    textentry3 = Entry(InputScreen,
                textvariable = silver_e, width = 5).place(x = 325, y = 110)

    textentry4 = Entry(InputScreen,
               textvariable = copper_e, width = 5).place(x = 325, y = 140)

    textentry5 = Entry(InputScreen,
               textvariable = oil_e, width = 5).place(x = 325, y = 170)

    textentry6 = Entry(InputScreen,
               textvariable = wheat_e, width = 5).place(x = 325, y = 200)

    textentry7 = Entry(InputScreen,
               textvariable = diesel_e, width = 5).place(x = 325, y = 230)

    textentry8 = Entry(InputScreen,
               textvariable = coco_e, width = 5).place(x = 325, y = 260)

    button3 = Button(InputScreen,
                     command = lambda: (Update(gold_e)),
                     text = "Update",
                     fg = "black",
                     bg = "red").place(x = 300, y = 300)


    def Update(values):
        print("qqw"+str(values.get())+"wer")
        gold_v = values.get()

        print(gold_v)

        Label19 = Label(InputScreen, text = gold_v,
                        fg = "black",
                        bg = "light blue").place(x = 390,y = 80)
        print(values.get())

I have spent over 5 hours having me and a friend go over this, and cannot spot the error: If you say rewrite this with classes, that's not an option. The idea of the code was to avoid writing our own classes.

The code works fine on it's own (Add Game(0) at the bottom of the main code block) until you call this file from another file passing in the 'value' into the Game function.

APK
  • 47
  • 2
  • 8
  • There are no `return` statements at all -- why would you expect your code to return anything? Perhaps you meant `print` rather than `return`? If so -- edit your question. – John Coleman Nov 09 '15 at 14:58
  • Yes, you are right sorry: I just ment that the code believes that the string var is empty (the text entry) – APK Nov 09 '15 at 15:01
  • I tried running your second code and got `SyntaxError` on your line `def Game(result)`. Is this the exact code that you're running? Please ensure that the code here demonstrates the problem you have. – Kevin Nov 09 '15 at 15:02
  • Okay I made the corrections - I copied the code wrong... – APK Nov 09 '15 at 15:02
  • Thanks, it runs now. When I execute it, the program ends immediately without doing anything. But that's what your first code does too. Is that the intended behavior? – Kevin Nov 09 '15 at 15:07
  • Thats because this is just one file from a 3 file program: basically a different file imports this file, and called the game function with update.Game(result) (Update is the file name) and result is an integer. – APK Nov 09 '15 at 15:10
  • It opens the InputScreen window however it believes the entries are always blank – APK Nov 09 '15 at 15:11
  • I doubt this is the direct cause of your problem, but you must not `place` a widget and assign it to a variable in a single statement; see [Tkinter: AttributeError: NoneType object has no attribute get](http://stackoverflow.com/q/1101750/953482) for more information. – Kevin Nov 09 '15 at 15:12
  • The code works fine on it's own (Add Game(0) at the bottom of the main code block) however when you call this file from another file passing in the 'value' into the Game function. And that's just sloppy coding on our part. – APK Nov 09 '15 at 15:14
  • We're going to have to see actual code that causes the actual error. We can't debug what we can't see. For example, is this other file creating its own root window? Are you using threading or multiprocessing? How do you "[pass] in the 'value' into the Game function"? – Bryan Oakley Nov 09 '15 at 15:39

1 Answers1

1

My guess is that you're creating more than one root window, since you say you are passing in the "value" from another file, and this "value" is an instance of a tkinter variable. Since you can't create one of these variables without creating a root window, this other file must be creating a root window.

Your whole tkinter program should only ever create exactly one root window. If you need to create more than one window you need to create instances of Toplevel.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685