Python Forum
How to print using wx
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print using wx
#1
Below is the code. I just need to know how to get the prints to print in the panel.

import wx
import mysql.connector
from mysql.connector import Error
import csv

panel = "global"
average = "global"
A1c = "global"


class bucky(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, None, wx.ID_ANY, 'A1c', size=(400, 400))
        panel = wx.Panel(self, wx.ID_ANY)

        button1 = wx.Button(panel, label='Import Data', pos=(10, 10), size=(75, 40))
        self.Bind(wx.EVT_BUTTON, self.importdata, button1)
        self.Bind(wx.EVT_CLOSE, self.closewindow)

        button2 = wx.Button(panel, label='A1c', pos=(85, 10), size=(75, 40))
        self.Bind(wx.EVT_BUTTON, self.showa1c, button2)
        self.Bind(wx.EVT_CLOSE, self.closewindow)

        button3 = wx.Button(panel, label='Exit', pos=(160, 10), size=(75, 40))
        self.Bind(wx.EVT_BUTTON, self.closebutton, button3)
        self.Bind(wx.EVT_CLOSE, self.closewindow)

        # wx.StaticText(panel, -1, "This is static text", (10, 65))
        #
        # custom = wx.StaticText(panel, -1, "This is custom static text", (10, 85), (260, -1,), wx.ALIGN_CENTER)
        # custom.SetForegroundColour('white')
        # custom.SetBackgroundColour('blue')

    def closebutton(self, event):
        self.Close(True)

    def closewindow(self, event):
        self.Destroy()

    def showa1c(self, event):
        try:
            con = mysql.connector.connect(host='localhost',
                                          database='meter',
                                          user='root',
                                          password='root')
            if con.is_connected():
                db_Info = con.get_server_info()
                print("Connected to MySQL database... MySQL Server version on ", db_Info)
                cursor = con.cursor()
                cursor.execute("select database();")
                record = cursor.fetchone()
                print("Your connected to - ", record)
        except Error as e:
            print("Error while connecting to MySQL", e)

        cursor = con.cursor()

        cursor.execute(
            "SELECT SUM(hist) FROM readings WHERE rdate > now() - interval 91 day AND rtype = 0")
        hsum = cursor.fetchone()[0]
        cursor.execute(
            "SELECT SUM(scan) FROM readings WHERE rdate > now() - interval 91 day AND rtype = 1")
        ssum = cursor.fetchone()[0]
        cursor.execute(
            "SELECT count(hist) FROM readings WHERE rtype = 0 AND rdate > now() - interval 91 day ")
        hcount = cursor.fetchone()[0]
        cursor.execute(
            "SELECT count(scan) FROM readings WHERE rtype = 1 AND rdate > now() - interval 91 day")
        scount = cursor.fetchone()[0]
        con.commit()
        con.close()

        tsum = hsum + ssum
        tall = hcount + scount
        average = round(tsum / tall)
        A1c = (46.7 + average) / 28.7

        print("Average: {}".format(average))
        print('A1c {0:.1f}'.format(A1c))

    def importdata(self, event):
        try:
            con = mysql.connector.connect(host='localhost',
                                          database='meter',
                                          user='root',
                                          password='root')
            if con.is_connected():
                db_Info = con.get_server_info()
                print("Connected to MySQL database... MySQL Server version on ", db_Info)
                cursor = con.cursor()
                cursor.execute("select database();")
                record = cursor.fetchone()
                print("Your connected to - ", record)
        except Error as e:
            print("Error while connecting to MySQL", e)

        cursor = con.cursor()

        # ----------------------------------- INSERT DATA -------------------------------------
        # cursor.execute('TRUNCATE TABLE readings')
        def inportdata():
            f = open('summary2.csv', 'r')
            dr = csv.reader(f, delimiter='\t')
            next(dr)
            next(dr)
            next(dr)
            for row in dr:
                if row[4] == '':
                    row[4] = 0
                if row[3] == '':
                    row[3] = 0
                cursor.execute(
                    'INSERT INTO readings (id, rdate, rtype, hist, scan, novalog, novalogu, food, carbs, lantus, lantusu, notes, strip, ketone, na1, na2, na3, ptime, utime) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',
                    row)
            con.commit()


if __name__ == '__main__':
    app = wx.App(False)
    frame = bucky(parent=None, id=-1)
    frame.Show()
    app.MainLoop()
As you see I tried to mess around but could not figure it out. Everython works except no output in panel when it runs.
Gary
Reply
#2
I figured it out.
Gary
Reply
#3
That is great to hear. :) You are welcome to share the solution with the community, for potential benefit to other coders.
Reply
#4
Here is the code I used. Worked nice. Big Grin


import mysql.connector
from mysql.connector import Error
import wx

average = 'global'
a1c = 'global'
try:
    con = mysql.connector.connect(host='localhost',
                                  database='meter',
                                  user='root',
                                  password='root')
    if con.is_connected():
        db_Info = con.get_server_info()
        print("Connected to MySQL database... MySQL Server version on ", db_Info)
        cursor = con.cursor()
        cursor.execute("select database();")
        record = cursor.fetchone()
        # print("Your connected to - ", record)
except Error as e:
    print("Error while connecting to MySQL", e)

cursor = con.cursor()

cursor.execute(
    "SELECT SUM(hist) FROM readings WHERE rdate > now() - interval 91 day AND rtype = 0")
hsum = cursor.fetchone()[0]
cursor.execute(
    "SELECT SUM(scan) FROM readings WHERE rdate > now() - interval 91 day AND rtype = 1")
ssum = cursor.fetchone()[0]
cursor.execute(
    "SELECT count(hist) FROM readings WHERE rtype = 0 AND rdate > now() - interval 91 day ")
hcount = cursor.fetchone()[0]
cursor.execute(
    "SELECT count(scan) FROM readings WHERE rtype = 1 AND rdate > now() - interval 91 day")
scount = cursor.fetchone()[0]
con.commit()
con.close()

tsum = hsum + ssum
tall = hcount + scount
average = round(tsum / tall)
a1c = round((46.7 + average) / 28.7, 2)


# ----------------------------------------------------------------------------------

class Example(wx.Frame):

    def __init__(self, *args, **kw):
        super(Example, self).__init__(*args, **kw)

        self.InitUI()

    def InitUI(self):
        pnl = wx.Panel(self)

        font = wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD)
        heading = wx.StaticText(self, label='Average Glucose and A1c',
                                pos=(25, 15), size=(200, -1))
        heading.SetFont(font)

        wx.StaticLine(self, pos=(25, 50), size=(300, 1))

        wx.StaticText(self, label='Average: ' + str(average), pos=(25, 80))
        wx.StaticText(self, label='A1c: ' + str(a1c), pos=(25, 100))

        wx.StaticLine(self, pos=(25, 260), size=(300, 1))

        btn = wx.Button(self, label='Close', pos=(140, 310))

        btn.Bind(wx.EVT_BUTTON, self.OnClose)

        self.SetSize((360, 380))
        self.SetTitle('wx.StaticLine')
        self.Centre()

    def OnClose(self, e):
        self.Close(True)


def main():
    app = wx.App()
    ex = Example(None)
    ex.Show()
    app.MainLoop()


if __name__ == '__main__':
    main()
Gary
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020