使用wxPython显示SQL查询结果

时间:2016-12-01 15:31:11

标签: python mysql sql python-2.7 wxpython

我试图将SQL查询的结果显示在文本旁边"最后24小时关闭的门票"脚本执行时。
SQL查询返回一个数字。 我试图在静态文本框中显示self.some_text的数字,它将标签文本更改为运行时的数字。

示例:https://s11.postimg.org/jil0kzfqb/tickets.png 在图像中,SQL查询中没有数字。 (您可以忽略地址,单元号和城市文本和框)

我要关闭其他人创建的程序,因此可能不需要这些代码。 我正在尝试动态学习SQL和Python,现在几乎是一个新手。

到目前为止,这是我的代码:

import wx
import MySQLdb

db = MySQLdb.connect(host="xxxxxxxxxxxx",port=xxx,user="xx",passwd="xxxxx",db="xxxx")

class Data():
    def TicketsClosedLast24Hours(self, event):
        cur = db.cursor()
        cur.execute("Select COUNT(*) FROM HD_TICKET WHERE HD_STATUS_ID = 12 AND TIME_CLOSED >= NOW() - INTERVAL 1 DAY")
        row = cur.fetchone()
        print(row[0])
        row = cur.fetchone()

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)

        self.Centre()
        self.Maximize(True)

        self.background = wx.Panel(self)
        self.background.SetBackgroundColour(wx.Colour(98, 125, 152))

        self.some_text = wx.StaticText(self.background, wx.ID_ANY, label="", style=wx.ALIGN_CENTER)
        self.Address = wx.TextCtrl(self.background, -1, "", size=(140, -1))
        self.UnitNumber = wx.TextCtrl(self.background, -1, "", size=(140, -1))
        self.City = wx.TextCtrl(self.background, -1, "", size=(140, -1))

        self.horizontalRow0 = wx.BoxSizer()
        img1 = wx.Image("logo1.bmp", wx.BITMAP_TYPE_BMP, -1)
        self.bitmap1 = wx.StaticBitmap(self.background, -1, wx.BitmapFromImage(img1), (55, 0))
        self.horizontalRow0.Add(self.bitmap1, wx.Center, border=10)

        self.VerticalRow1 = wx.BoxSizer(wx.VERTICAL)
        self.VerticalRow1.Add(wx.StaticText(self.background, -1, "Tickets Closed Last 24 Hours",
                                            style=wx.ALIGN_CENTER), flag=wx.ALL, border=5)
        self.VerticalRow1.Add(wx.StaticText(self.background, -1, "Address:",
                                            style=wx.ALIGN_CENTER), flag=wx.ALL, border=9)
        self.VerticalRow1.Add(wx.StaticText(self.background, -1, "Unit Number:",
                                            style=wx.ALIGN_CENTER), flag=wx.ALL, border=9)
        self.VerticalRow1.Add(wx.StaticText(self.background, -1, "City:",
                                            style=wx.ALIGN_CENTER), flag=wx.ALL, border=9)

        # To add labels - H-1 V-2
        self.VerticalRow2 = wx.BoxSizer(wx.VERTICAL)
        self.VerticalRow2.Add(self.some_text, proportion=1, flag=wx.ALL, border=6)
        self.VerticalRow2.Add(self.Address, proportion=1, flag=wx.ALL, border=6)
        self.VerticalRow2.Add(self.UnitNumber, proportion=1, flag=wx.ALL, border=6)
        self.VerticalRow2.Add(self.City, proportion=1, flag=wx.ALL, border=6)

        # Combine V-1 And V-2 to H1
        self.horizontalRow1 = wx.BoxSizer()
        self.horizontalRow1.Add((30, 30), proportion=0, flag=wx.EXPAND, border=0)
        self.horizontalRow1.Add(self.VerticalRow1, proportion=.5)
        self.horizontalRow1.Add(self.VerticalRow2, proportion=0)

        # To add labels - H-3 V-8
        self.VerticalRow1 = wx.BoxSizer(wx.VERTICAL)

        # To add labels - H-4 V-9
        self.horizontalRow4 = wx.BoxSizer()
        self.horizontalRow4.Add((30, 30), proportion=.5, flag=wx.EXPAND, border=0)
        self.horizontalRow4.Add((30, 30), proportion=1, flag=wx.EXPAND, border=0)

        # Adds horizontal Rows to Main Window in Vertical boxes
        self.verticalBox = wx.BoxSizer(wx.VERTICAL)
        self.verticalBox.Add(self.horizontalRow0, proportion=0, flag=wx.EXPAND, border=10)
        self.verticalBox.Add((10, 10), proportion=0, flag=wx.EXPAND, border=10)
        self.verticalBox.Add(self.horizontalRow1, proportion=0, flag=wx.EXPAND, border=0)
        self.verticalBox.Add((10, 10), proportion=0, flag=wx.EXPAND, border=10)
        self.verticalBox.Add(self.horizontalRow4, proportion=0, flag=wx.EXPAND, border=0)

        self.background.SetSizer(self.verticalBox)
        self.Show()

    def someFunction(self):
        mysql_data = databasemodel.returnData()  # query your database to return a string
        self.some_text.SetLabel(mysql_data)


if __name__ == '__main__':
    app = wx.App(redirect=False)
    window = MainFrame()
    app.MainLoop()

任何帮助都将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

代码对我来说是正确的。但是,我看不到任何实际调用someFunction的内容。我猜这就是问题所在。您应该尝试调用它并将mysql_data打印到stdout以确保您收到的结果符合您的预期。