简体   繁体   中英

Tk python code is ignored?

I'm making a GUI based game but for some reason a couple of lines in my code are not doing anything at all, when i know they should be.

def inspect():
     if 'camera' in inv:
     out1.config(text="there's nothing unusual here")
     darts()

For some reason, darts() and out1.config() are completely ignored in this specific part of the code.

I know the if statement runs as it should, I have used some print statements to make sure. the function is called at the right time, and inv does contain 'camera'.

I have used different text in out1.config(),but no matter what this line wont do anything.

the code simply continues on after as normal, as if the if statement never ran, and just does what would happen if the statement returned false. everything past then runs fine, everything before runs fine.

This bit of code worked perfect before, but just when i came back to do some more work on it it suddenly dosnt work.

Here is the GUI part:

global a , b , c , d ,out1 , main_menu,out2

main_menu.destroy()
window=gui.Tk()
global window

actions1 = gui.Frame(window)
out2 = gui.Label(window,text='Brefing room ; Cmdr Forge')
out1 = gui.Message(window,text='ok')
line = gui.Label(text='-------------------------------------------------------------')
a = gui.Button(window,text = 'a')
b = gui.Button(window,text = 'b')
c = gui.Button(window, text = 'c')
d = gui.Button(window, text = 'd')


out2.pack()
line.pack()
out1.pack(side='right')
actions1.pack()
a.pack()
b.pack()
c.pack()
d.pack()
start()
window.mainloop()

EVERYTHING else that uses this works as it should.

This is the part of code that inspect() is in:

def darts():
            def inspect():
                if 'camera' in inv:
                    out1.config(text="there's nothing unusual here")
                    darts()

                def inspect_glass():
                    def remove():
                        out1.config(text = 'you now have a minature video camera. If only you knew where to plug it in so you could see the footage.')
                        inv.append('camera')
                        darts()
                    out1.config (text="on closer inspection it appears to be a lens of a minature video camrea. it is not one of the agency's, it looks different from the ones they use.")
                    a.config (text='remove camrea',command = remove)
                out1.config(text='above the dartboord apears to be a small glass ball, pressed firmly into the wall')
                a.config(text='look closer',command = inspect_glass)
            a.config(text='look at dartboard', command = inspect)
            b.config(text='play darts', command = play_darts)
            c.config(text='do something elce', command = commons)
            out2.config(text='Common room ; darts')

The code basicly does this: if you look at the deartboard you see a glass ball. if you look closer you see its a camrea. you have the option to take it. ten if you were to look at the dartboard again, it should say nothing usual here, if you had already taken the camera.

def inspect():
    print '-*- HERE -*-'
    print inv
    print 'camera' in inv
    if 'camera' in inv:
        print '--> yes'
        out1.config(text="there's nothing unusual here")
        darts()
    else:
        print '--> no'

In case of doubts, try putting enough prints that you can track what exactly occurs. I bet that, unlike what you deduced, they will not output an inv with 'camera' in it but at the same print --> no .

A better idea is to replace all these prints with import pdb; pdb.set_trace() import pdb; pdb.set_trace() and then use the debugger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM