开发者

wxPython, variable not updating correctly

Hello my StackOverflow friends. I have been doing wxPython tutorials and reading the documentation, so far I like it. I wanted to make a simple app first. What this app would do is send a command to a micro controller to turn a relay on or off.

I have a global variable to get the COM port to be used (Instead of hardcoding COM1 in for instance)

I can't get that variable to be correctly updated. Here is the code:

#!/usr/bin/env python

import wx
from firmata import *

# Arduino(port, baudrate=115200)
#arduino = Arduino(comListenPort)
#arduino.pin_mode(7, firmata.OUTPUT)
comListenPort = 'Is not set'
getComPort = 'Not Set'

class ArduinoDemo(wx.Frame):
    def __init__(self, parent, id):
        # Main window creation
        wx.Frame.__init__(self, parent, id, 'Demonstration of Firmata', \
                          size = (300, 200))

        # Content
        mainPanel = wx.Panel(self)

        # Open Contact button creation
        relayOpen = wx.Button(mainPanel, label = 'Open Contact', \
                              pos = (25, 25), size = (120, 60))

        # Close Contact button creation
        relayClosed = wx.Button(mainPanel, label = 'Close Contact', \
                                pos = (150, 25), size = (120, 60))

        # Binds click event from relayOpen to openRelay
        self.Bind(wx.EVT_BUTTON, self.closeRelay, relayClosed)
        # Binds click event from relayClose to closeRelay
        self.Bind(wx.EVT_BUTTON, self.openRelay, relayOpen)

        # Get correct COM port开发者_开发问答
        getComPort = wx.TextEntryDialog(None, 'Enter COM Port', 'COM Port', '')
        if getComPort.ShowModal() == wx.ID_OK:
            comListenPort = getComPort.GetValue()
            # # Debug
            print getComPort.GetValue()
            print comListenPort
            # # /Debug


    def openRelay(self, event):
        #arduino.digital_write(7, firmata.HIGH)
        # # Debug
        print comListenPort # does not print correctly
        # # /Debug

    def closeRelay(self, event):
        #arduino.digital_write(7, firmata.LOW)
        # # Debug
        print getComPort # does not print correctly
        # # /Debug


if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = ArduinoDemo(parent = None, id = -1)
    frame.Show()
    app.MainLoop()

Now I assume that having a global variable for this would the best way to do it, but I am completely looking for suggestions and pointers in everything. To the point though, comListenPort does not get assigned the value of my TextEntryDialog box. I know this has to be the dumbest thing I am overlooking.

The two debug print statements getComPort.GetValue() and comListenPort both print the correct data. When I click either the relayOpen or relayClosed button, they say 'is not set' or 'not set' still. I hope someone can slap some sense into me, I am pretty dumbfounded (emphasis on DUMB)

Thanks again


I think that inside __init__ Python sees comListenPort as a local variable, not global.. You should declare it global before using it:

global comListenPort
comListenPort = getComPort.GetValue()

Alternatively, you could store the value as an instance variable of ArduinoDemo:

self.comListenPort = getComPort.GetValue()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜