Python: variable only works when print statement is present

孤街浪徒 提交于 2020-06-29 08:20:27

问题


I have an extremely strange problem that has stumped me for about the past day.

import wx, gui, threading, time, pyodbc
from OrderNumEntry import entryID, plyID, stationID
from main import strPlyDB
from Op1Login import eID1
global eID1

class MainFrame1(gui.MainFrame1Op):
    def __init__(self, parent):
        gui.MainFrame1Op.__init__(self, parent)
        self.m_textCtrl3.SetValue(eID1)
        # print eID1

    def checkClick1(self, event):
        if threading.activeCount() < 2:
            t1 = threading.Thread(target = self.checkAdvance1)
            t1.setDaemon(True)
            t1.start()
            self.m_checkBox110.SetValue(True)

    def checkAdvance1(self):
        strODBC_conn = 'DRIVER={Microsoft Access Driver (*.mdb)};DBQ=%s' % strPlyDB
        conn = pyodbc.connect(strODBC_conn)
        cursor = conn.cursor()

        # eID1 = '00839530'
        print 'test1'
        print eID1
        print 'test2'

        SQL = 'INSERT INTO tblRecord(EntryID, PlyID, StationID, EmployeeNum, BuyoffTime) values(?,?,?,?,?)'
        cursor.execute(SQL, (entryID, plyID, stationID, eID1, time.strftime("%c")))

        cursor.commit()
        cursor.close()
        conn.close()

What's going on is there's a primary Python file main.py that initializes my mainframe1 class, which references gui.py for all of its wxpython code to bring up the window. This file is simply the event handler. checkClick1 runs when I click on a check box in my window (creative name I know), which in turn starts a thread for checkAdvance1.

Everything works fine in the program except for one part. My eID1 variable works just fine in __init__, and textCtrl3 displays it. However, when I try and use it again in checkAdvance1, the program hangs.

To test it, I surrounded the print statement for eID1 with two test print statements. Only test1 is printed, and no error code is given, the program simply hangs. To make matters even more confusing, when I put a print eID1 statement in the __init__ function (it's commented out right now), the program runs just fine!

I'm not sure how variable functionality could be affected by a print statement, but it's happening. However, putting a print statement in to fix it seems like a shoddy workaround that ignores the actual problem. Also, the commented-out eID1 declaration is just an example to show you what the content of the variable looks like when the program is running.

EDIT: I have come up with a work around that I'm not entirely satisfied with, but it's better than adding print. If I create another variable, I can set it to the string translation of empID1, like so: empID1 = str(eID1). I can then reference this value later in place of eID1. However, this still doesn't sit well with me, as according to everything I know and have researched, my original code SHOULD work. It makes me feel like there's something bad going on behind the scenes that could come back to bite me later.

来源:https://stackoverflow.com/questions/25938453/python-variable-only-works-when-print-statement-is-present

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!