Query result in wxtext ctrl

Hy all, i try to connect with mysql server, i do a query a i print the query result on screen. Now i want to print this result in a three wxtextctrl i try with this code but is non correct!!
Help me please.

def connetti(self, event):
        try:
            db = MySQLdb.connect(host='localhost', user='ciccio', db='prova', passwd='pino')
            print "Sei connesso"
        except:
            print "Failed"
        try:
            cursor = db.cursor()
            n = self.ctrl_ricerca.GetValue()
            interrogazione = 'SELECT * FROM rubrica WHERE id=%s' % (n)
            cursor.execute(interrogazione)
            row = cursor.fetchone()
            result = row[0], row[1], row[2]
            print result
        except:
            print "Failed"
            self.ctrltext1.WriteText(row[0]) self.ctrltext2.WriteText(row[1])
            self.ctrltext3.WriteText(row[2])

Ghido

Just a thought, but since you are using WriteText, have to set the current insert position anywhere?

If so, what is the exact error other than "not correct", can you explain a little more, error codes and such?

Thanks
D

···

At 07:02 AM 12/5/2003, you wrote:

Hy all, i try to connect with mysql server, i do a query a i print the query result on screen. Now i want to print this result in a three wxtextctrl i try with this code but is non correct!!
Help me please.

def connetti(self, event):
       try:
           db = MySQLdb.connect(host='localhost', user='ciccio', db='prova', passwd='pino')
           print "Sei connesso"
       except:
           print "Failed"
       try:
           cursor = db.cursor()
           n = self.ctrl_ricerca.GetValue()
           interrogazione = 'SELECT * FROM rubrica WHERE id=%s' % (n)
           cursor.execute(interrogazione)
           row = cursor.fetchone()
           result = row[0], row[1], row[2]
           print result
       except:
           print "Failed"
           self.ctrltext1.WriteText(row[0])
           self.ctrltext2.WriteText(row[1])
           self.ctrltext3.WriteText(row[2])

Ghido

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

Sorry: i haven't an error but the result don't print in wxctrltext.
and this is all my code:

#!/usr/bin/env python
# generated by wxGlade 0.3.2cvs on Fri Dec 5 11:46:41 2003

from wxPython.wx import *
import sys
import MySQLdb
import os
import wx
ID_EXIT = wxNewId()
ID_CONNETTI = wxNewId()
ID_SEARCH = wxNewId()
class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)

        # Menu Bar
        self.main_menubar = wxMenuBar()
        self.SetMenuBar(self.main_menubar)
        self.File = wx.Menu()
        self.File.Append(ID_EXIT, "Esci", "Esci dall'applicazione", wx.ITEM_NORMAL)
        self.File.Append(ID_CONNETTI, "Connetti", "Collegati al database", wx.ITEM_NORMAL)
        self.main_menubar.Append(self.File, "File")
        # Menu Bar end
        self.main_statusbar = self.CreateStatusBar(1)
        self.cella1 = wx.TextCtrl(self, -1, "")
        self.cella2 = wx.TextCtrl(self, -1, "")
        self.cella3 = wx.TextCtrl(self, -1, "")
        self.Ricerca = wx.StaticText(self, -1, "Ricerca")
        self.ctrl_ricerca = wx.TextCtrl(self, -1, "")
        self.search = wx.Button(self, ID_SEARCH, "search")

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("frame_1")
        self.SetSize((400, 300))
        self.main_statusbar.SetStatusWidths([-1])
        # statusbar fields
        main_statusbar_fields = ["main_statusbar"]
        for i in range(len(main_statusbar_fields)):
            self.main_statusbar.SetStatusText(main_statusbar_fields[i], i)
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        grid_sizer_1 = wx.GridSizer(12, 3, 0, 0)
        grid_sizer_1.Add(self.cella1, 0, 0, 0)
        grid_sizer_1.Add(self.cella2, 0, 0, 0)
        grid_sizer_1.Add(self.cella3, 0, 0, 0)
        grid_sizer_1.Add(self.Ricerca, 0, wx.ALIGN_BOTTOM, 0)
        grid_sizer_1.Add(self.ctrl_ricerca, 0, wx.ALIGN_BOTTOM, 0)
        grid_sizer_1.Add(self.search, 0, wx.ALIGN_BOTTOM, 0)
        self.SetAutoLayout(1)
        self.SetSizer(grid_sizer_1)
        self.Layout()
        # end wxGlade
#definiamo le azioni
    EVT_MENU(self, ID_EXIT, self.esci)
        EVT_MENU(self, ID_CONNETTI, self.connetti)
    EVT_BUTTON(self, ID_SEARCH, self.connetti)
    def esci(self, event):
        self.Close(1)
    def connetti(self, event):
    try:
        db = MySQLdb.connect(host='localhost', user='root', db='prova', passwd='')
        print "Sei connesso"
    except:
        print "Failed"
    try:
            cursor = db.cursor()
            n = self.ctrl_ricerca.GetValue()
        interrogazione = 'SELECT * FROM rubrica WHERE id=%s' % (n)
        cursor.execute(interrogazione)
        row = cursor.fetchone()
        result = row[0], row[1], row[2]
        print result
    except:
        print "Failed"
        self.cella1.WriteText(row[0])
        self.cella2.WriteText(row[1])
        self.cella3.WriteText(row[2])
# end of class MyFrame

class MyApp(wx.App):
    def OnInit(self):
        wx.InitAllImageHandlers()
        main = MyFrame(None, -1, "")
        self.SetTopWindow(main)
        main.Show(1)
        return 1

# end of class MyApp

if __name__ == "__main__":
    digigest = MyApp(0)
    digigest.MainLoop()

Paolo,

Looking at this piece of your code:

    try:
            cursor = db.cursor()
            n = self.ctrl_ricerca.GetValue()
        interrogazione = 'SELECT * FROM rubrica WHERE id=%s' % (n)
        cursor.execute(interrogazione)
        row = cursor.fetchone()
        result = row[0], row[1], row[2]
        print result
    except:
        print "Failed"
        self.cella1.WriteText(row[0])
        self.cella2.WriteText(row[1])
        self.cella3.WriteText(row[2])
# end of class MyFrame

It looks to me like you only put text in the cella1, cella2, and cella3
variables if an exception is raised, not if your query is successful.

Try:

    try:
            cursor = db.cursor()
            n = self.ctrl_ricerca.GetValue()
        interrogazione = 'SELECT * FROM rubrica WHERE id=%s' % (n)
        cursor.execute(interrogazione)
        row = cursor.fetchone()
        self.cella1.WriteText(row[0])
        self.cella2.WriteText(row[1])
        self.cella3.WriteText(row[2])
        result = row[0], row[1], row[2]
        print result
    except:
        print "Failed"
# end of class MyFrame

David

···

-----Original Message-----
From: Paolo Ghidini [mailto:paolo.ghidini@heroflon.it]
Sent: Friday, December 05, 2003 7:17 AM
To: wxPython-users@lists.wxwindows.org
Subject: Re: [wxPython-users] Query result in wxtext ctrl

Sorry: i haven't an error but the result don't print in wxctrltext.
and this is all my code:

#!/usr/bin/env python
# generated by wxGlade 0.3.2cvs on Fri Dec 5 11:46:41 2003

from wxPython.wx import *
import sys
import MySQLdb
import os
import wx
ID_EXIT = wxNewId()
ID_CONNETTI = wxNewId()
ID_SEARCH = wxNewId()
class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)

        # Menu Bar
        self.main_menubar = wxMenuBar()
        self.SetMenuBar(self.main_menubar)
        self.File = wx.Menu()
        self.File.Append(ID_EXIT, "Esci", "Esci dall'applicazione",
wx.ITEM_NORMAL)
        self.File.Append(ID_CONNETTI, "Connetti", "Collegati al
database", wx.ITEM_NORMAL)
        self.main_menubar.Append(self.File, "File")
        # Menu Bar end
        self.main_statusbar = self.CreateStatusBar(1)
        self.cella1 = wx.TextCtrl(self, -1, "")
        self.cella2 = wx.TextCtrl(self, -1, "")
        self.cella3 = wx.TextCtrl(self, -1, "")
        self.Ricerca = wx.StaticText(self, -1, "Ricerca")
        self.ctrl_ricerca = wx.TextCtrl(self, -1, "")
        self.search = wx.Button(self, ID_SEARCH, "search")

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("frame_1")
        self.SetSize((400, 300))
        self.main_statusbar.SetStatusWidths([-1])
        # statusbar fields
        main_statusbar_fields = ["main_statusbar"]
        for i in range(len(main_statusbar_fields)):
            self.main_statusbar.SetStatusText(main_statusbar_fields[i], i)
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        grid_sizer_1 = wx.GridSizer(12, 3, 0, 0)
        grid_sizer_1.Add(self.cella1, 0, 0, 0)
        grid_sizer_1.Add(self.cella2, 0, 0, 0)
        grid_sizer_1.Add(self.cella3, 0, 0, 0)
        grid_sizer_1.Add(self.Ricerca, 0, wx.ALIGN_BOTTOM, 0)
        grid_sizer_1.Add(self.ctrl_ricerca, 0, wx.ALIGN_BOTTOM, 0)
        grid_sizer_1.Add(self.search, 0, wx.ALIGN_BOTTOM, 0)
        self.SetAutoLayout(1)
        self.SetSizer(grid_sizer_1)
        self.Layout()
        # end wxGlade
#definiamo le azioni
    EVT_MENU(self, ID_EXIT, self.esci)
        EVT_MENU(self, ID_CONNETTI, self.connetti)
    EVT_BUTTON(self, ID_SEARCH, self.connetti)
    def esci(self, event):
        self.Close(1)
    def connetti(self, event):
    try:
        db = MySQLdb.connect(host='localhost', user='root', db='prova',
passwd='')
        print "Sei connesso"
    except:
        print "Failed"
    try:
            cursor = db.cursor()
            n = self.ctrl_ricerca.GetValue()
        interrogazione = 'SELECT * FROM rubrica WHERE id=%s' % (n)
        cursor.execute(interrogazione)
        row = cursor.fetchone()
        result = row[0], row[1], row[2]
        print result
    except:
        print "Failed"
        self.cella1.WriteText(row[0])
        self.cella2.WriteText(row[1])
        self.cella3.WriteText(row[2])
# end of class MyFrame

class MyApp(wx.App):
    def OnInit(self):
        wx.InitAllImageHandlers()
        main = MyFrame(None, -1, "")
        self.SetTopWindow(main)
        main.Show(1)
        return 1

# end of class MyApp

if __name__ == "__main__":
    digigest = MyApp(0)
    digigest.MainLoop()

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

Thank you very much David it's ok!!!!

···

--
Paolo Ghidini
HEROFLON Srl
QAS-EMS & EDP Manager
Tel. 0039302510211
Fax 0039302511495
paolo.ghidini@heroflon.it
www.heroflon.it

#############################################################
This E-mail and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they are
addressed. If you have received this E-mail in error, please notify
info@heroflon.it. Any disclosure, copying, reproduction distribution or
other use of the E-mail is prohibited.
Neither the Heroflon Srl nor any individual sending this E-mail accepts any
liability in respect of the content (including errors and omissions)
#############################################################

Hello.

I am trying to implement a wxSlider control, but am running into difficulty.
The slider's GetValue() method returns the default value rather than the
value that reflects the slider position. (I'm using wxPython 2.4.2.4 on
Win2K.) That renders the slider rather useless.

I've attached a code sample below.

Can anyone see what I'm doing wrong? Have I implemented the Scroll Event
incorrectly? Or is that is bug in 2.4.2.4?

Thanks in advance,

David Woods, Ph.D.
Wisconsin Center for Education Research
University of Wisconsin, Madison
http://www.transana.org

# Copyright (C) 2003 The Board of Regents of the University of Wisconsin
System

···

#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

"""This file implements the OptionsSettings class, which defines the
   main Options > Settings Dialog Box and values. """

__author__ = 'David Woods <dwoods@wcer.wisc.edu>'

from wxPython import wx
import gettext

_ = gettext.gettext

class OptionsSettings(wx.wxDialog):
    """ Options > Settings Dialog Box """

    def __init__(self, parent):
        """ Initialize the Program Options Dialog Box """
        wx.wxDialog.__init__(self, parent, -1, _("Transana Settings"),
wx.wxDefaultPosition, wx.wxSize(500, 240), style=wx.wxCAPTION |
wx.wxSYSTEM_MENU | wx.wxTHICK_FRAME)

        # Define a wxNotebook for the Tab structure
        lay = wx.wxLayoutConstraints()
        lay.top.SameAs(self, wx.wxTop, 0)
        lay.left.SameAs(self, wx.wxLeft, 0)
        lay.right.SameAs(self, wx.wxRight, 0)
        lay.bottom.SameAs(self, wx.wxBottom, 27)
        notebook = wx.wxNotebook(self, -1, size=self.GetSizeTuple()) #,
style=wx.wxNB_FIXEDWIDTH)
        notebook.SetConstraints(lay)

        panelTranscriber = wx.wxPanel(self, -1,
size=notebook.GetSizeTuple())

        # Add the Video Setback widgets to the Transcriber Settings Tab
        lblTranscriptionSetback = wx.wxStaticText(panelTranscriber, -1,
_("Transcription Setback: (Auto-rewind interval for Ctrl-S)"),
style=wx.wxST_NO_AUTORESIZE)
        lay = wx.wxLayoutConstraints()
        lay.top.SameAs(panelTranscriber, wx.wxTop, 10)
        lay.left.SameAs(panelTranscriber, wx.wxLeft, 10)
        lay.width.AsIs()
        lay.height.AsIs()
        lblTranscriptionSetback.SetConstraints(lay)

        choiceList = ['0', '2', '5']
        self.transcriptionSetback = wx.wxChoice(panelTranscriber, -1,
size=wx.wxSize(35, 25), choices=choiceList)
        self.transcriptionSetback.SetSelection(1)
        lay = wx.wxLayoutConstraints()
        lay.top.Below(lblTranscriptionSetback, 3)
        lay.left.SameAs(panelTranscriber, wx.wxLeft, 10)
        lay.width.AsIs()
        lay.height.AsIs()
        self.transcriptionSetback.SetConstraints(lay)

        # Add the Video Speed Slider widget to the Transcriber Settings Tab
        lblVideoSpeed = wx.wxStaticText(panelTranscriber, -1, _("Video
Playback Speed"), style=wx.wxST_NO_AUTORESIZE)
        lay = wx.wxLayoutConstraints()
        lay.top.Below(self.transcriptionSetback, 10)
        lay.left.SameAs(panelTranscriber, wx.wxLeft, 10)
        lay.width.AsIs()
        lay.height.AsIs()
        lblVideoSpeed.SetConstraints(lay)

        self.videoSpeed = wx.wxSlider(panelTranscriber, -1, 10, 1, 20,
style=wx.wxSL_HORIZONTAL | wx.wxSL_AUTOTICKS)
        lay = wx.wxLayoutConstraints()
        lay.top.Below(lblVideoSpeed, 3)
        lay.left.SameAs(panelTranscriber, wx.wxLeft, 10)
        lay.right.SameAs(panelTranscriber, wx.wxRight, 10)
        lay.height.AsIs()
        self.videoSpeed.SetConstraints(lay)

        self.lblVideoSpeedSetting = wx.wxStaticText(panelTranscriber, -1,
'1.0')
        lay = wx.wxLayoutConstraints()
        lay.top.Below(self.transcriptionSetback, 10)
        lay.right.SameAs(panelTranscriber, wx.wxRight, 10)
        lay.width.AsIs()
        lay.height.AsIs()
        self.lblVideoSpeedSetting.SetConstraints(lay)

        self.videoSpeed = wx.wxSlider(panelTranscriber, -1, 10, 1, 20,
style=wx.wxSL_HORIZONTAL | wx.wxSL_AUTOTICKS)
        lay = wx.wxLayoutConstraints()
        lay.top.Below(lblVideoSpeed, 3)
        lay.left.SameAs(panelTranscriber, wx.wxLeft, 10)
        lay.right.SameAs(panelTranscriber, wx.wxRight, 10)
        lay.height.AsIs()
        self.videoSpeed.SetConstraints(lay)

        wx.EVT_SCROLL(self, self.OnScroll)

        lblVideoSpeedMin = wx.wxStaticText(panelTranscriber, -1, _("0.1"),
style=wx.wxST_NO_AUTORESIZE)
        lay = wx.wxLayoutConstraints()
        lay.top.Below(self.videoSpeed, 2)
        lay.left.SameAs(self.videoSpeed, wx.wxLeft, 0)
        lay.width.AsIs()
        lay.height.AsIs()
        lblVideoSpeedMin.SetConstraints(lay)

        lblVideoSpeed1 = wx.wxStaticText(panelTranscriber, -1, _("1.0"),
style=wx.wxST_NO_AUTORESIZE)
        lay = wx.wxLayoutConstraints()
        lay.top.Below(self.videoSpeed, 2)
        # The "center" (1.0) position is 47% (9 / 19) of the way between 0.1
and 2.0. However, 48% looks better on Windows.
        lay.left.PercentOf(self.videoSpeed, wx.wxWidth, 48)
        lay.width.AsIs()
        lay.height.AsIs()
        lblVideoSpeed1.SetConstraints(lay)

        lblVideoSpeedMax = wx.wxStaticText(panelTranscriber, -1, _("2.0"),
style=wx.wxST_NO_AUTORESIZE)
        lay = wx.wxLayoutConstraints()
        lay.top.Below(self.videoSpeed, 2)
        lay.right.SameAs(self.videoSpeed, wx.wxRight, 0)
        lay.width.AsIs()
        lay.height.AsIs()
        lblVideoSpeedMax.SetConstraints(lay)

        panelTranscriber.SetAutoLayout(wx.true)
        panelTranscriber.Layout()

        notebook.AddPage(panelTranscriber, _("Transcriber Settings"),
wx.true)

        # Define the buttons on the bottom of the form
        lay = wx.wxLayoutConstraints()
        lay.top.Below(notebook, 3)
        lay.width.Absolute(85)
        lay.left.SameAs(self, wx.wxRight, -268)
        lay.bottom.SameAs(self, wx.wxBottom, 0)
        btnOK = wx.wxButton(self, -1, _('OK'))
        btnOK.SetConstraints(lay)

        lay = wx.wxLayoutConstraints()
        lay.top.Below(notebook, 3)
        lay.width.Absolute(85)
        lay.left.RightOf(btnOK, 6)
        lay.bottom.SameAs(self, wx.wxBottom, 0)
        btnCancel = wx.wxButton(self, -1, _('Cancel'))
        btnCancel.SetConstraints(lay)

        # Attach events to the Buttons
        wx.EVT_BUTTON(self, btnOK.GetId(), self.OnOK)
        wx.EVT_BUTTON(self, btnCancel.GetId(), self.OnCancel)

        self.Layout()
        self.SetAutoLayout(wx.true)
        self.ShowModal()

    def OnOK(self, event):
        """ Method attached to the 'OK' Button """
        # TODO: We need to save the values set here, and load them on
Program Startup.
        self.Close()

    def OnCancel(self, event):
        """ Method attached to the 'Close' Button """
        self.Close()

    def OnScroll(self, event):
        print "OnScroll = %g", self.videoSpeed.GetValue()
        self.lblVideoSpeedSetting.SetLabel("%1.1f" %
(float(self.videoSpeed.GetValue()) / 10))

class MyApp(wx.wxApp):
    def OnInit(self):
        frame = OptionsSettings(None)
        self.SetTopWindow(frame)
        return True

app = MyApp(0)
app.MainLoop()

David Woods wrote:

Hello.

I am trying to implement a wxSlider control, but am running into difficulty.
The slider's GetValue() method returns the default value rather than the
value that reflects the slider position. (I'm using wxPython 2.4.2.4 on
Win2K.) That renders the slider rather useless.

I've attached a code sample below.

Can anyone see what I'm doing wrong? Have I implemented the Scroll Event
incorrectly? Or is that is bug in 2.4.2.4?

You are creating two sliders, positioned one atop the other. You're then getting the events from the first one but reading the value from the second one.

Also, panelTranscriber needs to be a child of the notebook.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!