I am making a twitter client in python and wxpython. As of now, my static text is not printing. How can I change this code so it will print out what I want it to?

import wx
import twitter

class main(wx.Frame):
    
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id, "Twitter Client", size=(700,400))
        panel=wx.Panel(self)
        
        menubar = wx.MenuBar()
        file = wx.Menu()
        addaccount = wx.MenuItem(file, 1, '&Add Account\tCtrl+A')
        quit = wx.MenuItem(file, 2, '&Quit\tCtrl+Q')
        
        file.AppendItem(addaccount)
        file.AppendItem(quit)
        
        menubar.Append(file, '&File')
        self.SetMenuBar(menubar)
        self.Centre()
        self.Show(True)
        
        #Bind Events to Menu Item
        self.Bind(wx.EVT_MENU, self.info_user, id=1)
        self.Bind(wx.EVT_MENU, self.OnQuit, id=2)

        
    #Events
    def OnQuit(self, event):
        self.Close()

    def info_user(self, event):
        accountbox_username = wx.TextEntryDialog(None,"Twitter Username","")
        if accountbox_username.ShowModal()==wx.ID_OK:
            username = accountbox_username.GetValue()
            if username != "":
                accountbox_password = wx.TextEntryDialog(None,"Twitter Password","")
                if accountbox_password.ShowModal()==wx.ID_OK:
                    password = accountbox_password.GetValue()
                    if password != "":
                        password = accountbox_password.GetValue()
                        
                        #login to twitter
                        client = twitter.Api(username=username, password=password)
                        client.PostUpdate("Hello World!")
                        panel=wx.Panel(self)
                        #print friends
                        userlist = client.GetFriends()
                        for username in userlist:
                            wx.StaticText(panel, -1, username.screen_name, (10,100))
                            



                    else:
                        sorry = wx.MessageDialog(None, 'Your username or password cannot be blank','There was a problem...', wx.OK)
                        problemanswer = sorry.ShowModal()
            else:
                sorry = wx.MessageDialog(None, 'Your username or password cannot be blank','There was a problem...', wx.OK)
                problemanswer = sorry.ShowModal()

if  __name__=='__main__':
    app=wx.App()
    frame=main(parent=None,id=-1)
    app.MainLoop()

I am trying to print out username.screen_name

in this line where do you get username.screen_name?

wx.StaticText(panel, -1, username.screen_name, (10,100))

change to something like this

#
for username in userlist:
#
wx.StaticText(panel, -1, username, size = (10,100))

in this line where do you get username.screen_name?

wx.StaticText(panel, -1, username.screen_name, (10,100))

change to something like this

#
for username in userlist:
#
wx.StaticText(panel, -1, username, size = (10,100))

Sadly it didn't work. I think the problem with what you suggested is that the group is called username and the actual thing I want to print out is screen_name. Thanks though :D

What does userlist look like?
You seem to constantly overwrite the result in a newly created label at the same position.

What does userlist look like?
You seem to constantly overwrite the result in a newly created label at the same position.

Userlist is all of the info about that certain twitter user's friends. The screen_name specifies that the screen name should be used from userlist.

Can you test print userlist and show us the result?
(change app=wx.App() to app=wx.App(False) to do so)
Do you get any output in the label?

This what you have simplified ...

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, -1, mytitle, size=mysize)
        self.SetBackgroundColour("white")

        names = ['Fred', 'Olaf', 'Inge']
        for name in names:
            # will 'over-label' all the time, only last name shows
            wx.StaticText(self, -1, name)


app = wx.App(0)
MyFrame(None, 'wx.StaticText() label', (400, 100)).Show()
app.MainLoop()

This is how you should do it ...

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, -1, mytitle, size=mysize)
        self.SetBackgroundColour("white")

        names = ['Fred', 'Olaf', 'Inge']
        text = ""
        for name in names:
            text += name + '\n'
        wx.StaticText(self, -1, text)


app = wx.App(0)
MyFrame(None, 'wx.StaticText() label', (400, 100)).Show()
app.MainLoop()

Edited 6 Years Ago by vegaseat: n/a

Can you test print userlist and show us the result?
(change app=wx.App() to app=wx.App(False) to do so)
Do you get any output in the label?

I don't think I explained this clearly enough. I didn't make a list with the user info in it. I am getting it from the twitter database. Read this: http://www.tuxradar.com/content/code-project-create-python-twitter-bot

Ok I ran print userlist in the python shell and I got:

[<twitter.User object at 0x82515cc>, <twitter.User object at 0x825160c>, <twitter.User object at 0x825164c>, <twitter.User object at 0x825168c>, <twitter.User object at 0x82516cc>, <twitter.User object at 0x825170c>, <twitter.User object at 0x825174c>, <twitter.User object at 0x825178c>, <twitter.User object at 0x82517cc>, <twitter.User object at 0x825180c>, <twitter.User object at 0x825184c>, <twitter.User object at 0x825188c>, <twitter.User object at 0x82518cc>, <twitter.User object at 0x825190c>, <twitter.User object at 0x825194c>]
[<twitter.User object at 0x82515cc>, <twitter.User object at 0x825160c>, <twitter.User object at 0x825164c>, <twitter.User object at 0x825168c>, <twitter.User object at 0x82516cc>, <twitter.User object at 0x825170c>, <twitter.User object at 0x825174c>, <twitter.User object at 0x825178c>, <twitter.User object at 0x82517cc>, <twitter.User object at 0x825180c>, <twitter.User object at 0x825184c>, <twitter.User object at 0x825188c>, <twitter.User object at 0x82518cc>, <twitter.User object at 0x825190c>, <twitter.User object at 0x825194c>]
[<twitter.User object at 0x82515cc>, <twitter.User object at 0x825160c>, <twitter.User object at 0x825164c>, <twitter.User object at 0x825168c>, <twitter.User object at 0x82516cc>, <twitter.User object at 0x825170c>, <twitter.User object at 0x825174c>, <twitter.User object at 0x825178c>, <twitter.User object at 0x82517cc>, <twitter.User object at 0x825180c>, <twitter.User object at 0x825184c>, <twitter.User object at 0x825188c>, <twitter.User object at 0x82518cc>, <twitter.User object at 0x825190c>, <twitter.User object at 0x825194c>]
[<twitter.User object at 0x82515cc>, <twitter.User object at 0x825160c>, <twitter.User object at 0x825164c>, <twitter.User object at 0x825168c>, <twitter.User object at 0x82516cc>, <twitter.User object at 0x825170c>, <twitter.User object at 0x825174c>, <twitter.User object at 0x825178c>, <twitter.User object at 0x82517cc>, <twitter.User object at 0x825180c>, <twitter.User object at 0x825184c>, <twitter.User object at 0x825188c>, <twitter.User object at 0x82518cc>, <twitter.User object at 0x825190c>, <twitter.User object at 0x825194c>]
[<twitter.User object at 0x82515cc>, <twitter.User object at 0x825160c>, <twitter.User object at 0x825164c>, <twitter.User object at 0x825168c>, <twitter.User object at 0x82516cc>, <twitter.User object at 0x825170c>, <twitter.User object at 0x825174c>, <twitter.User object at 0x825178c>, <twitter.User object at 0x82517cc>, <twitter.User object at 0x825180c>, <twitter.User object at 0x825184c>, <twitter.User object at 0x825188c>, <twitter.User object at 0x82518cc>, <twitter.User object at 0x825190c>, <twitter.User object at 0x825194c>]
[<twitter.User object at 0x82515cc>, <twitter.User object at 0x825160c>, <twitter.User object at 0x825164c>, <twitter.User object at 0x825168c>, <twitter.User object at 0x82516cc>, <twitter.User object at 0x825170c>, <twitter.User object at 0x825174c>, <twitter.User object at 0x825178c>, <twitter.User object at 0x82517cc>, <twitter.User object at 0x825180c>, <twitter.User object at 0x825184c>, <twitter.User object at 0x825188c>, <twitter.User object at 0x82518cc>, <twitter.User object at 0x825190c>, <twitter.User object at 0x825194c>]
[<twitter.User object at 0x82515cc>, <twitter.User object at 0x825160c>, <twitter.User object at 0x825164c>, <twitter.User object at 0x825168c>, <twitter.User object at 0x82516cc>, <twitter.User object at 0x825170c>, <twitter.User object at 0x825174c>, <twitter.User object at 0x825178c>, <twitter.User object at 0x82517cc>, <twitter.User object at 0x825180c>, <twitter.User object at 0x825184c>, <twitter.User object at 0x825188c>, <twitter.User object at 0x82518cc>, <twitter.User object at 0x825190c>, <twitter.User object at 0x825194c>]
[<twitter.User object at 0x82515cc>, <twitter.User object at 0x825160c>, <twitter.User object at 0x825164c>, <twitter.User object at 0x825168c>, <twitter.User object at 0x82516cc>, <twitter.User object at 0x825170c>, <twitter.User object at 0x825174c>, <twitter.User object at 0x825178c>, <twitter.User object at 0x82517cc>, <twitter.User object at 0x825180c>, <twitter.User object at 0x825184c>, <twitter.User object at 0x825188c>, <twitter.User object at 0x82518cc>, <twitter.User object at 0x825190c>, <twitter.User object at 0x825194c>]
[<twitter.User object at 0x82515cc>, <twitter.User object at 0x825160c>, <twitter.User object at 0x825164c>, <twitter.User object at 0x825168c>, <twitter.User object at 0x82516cc>, <twitter.User object at 0x825170c>, <twitter.User object at 0x825174c>, <twitter.User object at 0x825178c>, <twitter.User object at 0x82517cc>, <twitter.User object at 0x825180c>, <twitter.User object at 0x825184c>, <twitter.User object at 0x825188c>, <twitter.User object at 0x82518cc>, <twitter.User object at 0x825190c>, <twitter.User object at 0x825194c>]
[<twitter.User object at 0x82515cc>, <twitter.User object at 0x825160c>, <twitter.User object at 0x825164c>, <twitter.User object at 0x825168c>, <twitter.User object at 0x82516cc>, <twitter.User object at 0x825170c>, <twitter.User object at 0x825174c>, <twitter.User object at 0x825178c>, <twitter.User object at 0x82517cc>, <twitter.User object at 0x825180c>, <twitter.User object at 0x825184c>, <twitter.User object at 0x825188c>, <twitter.User object at 0x82518cc>, <twitter.User object at 0x825190c>, <twitter.User object at 0x825194c>]
[<twitter.User object at 0x82515cc>, <twitter.User object at 0x825160c>, <twitter.User object at 0x825164c>, <twitter.User object at 0x825168c>, <twitter.User object at 0x82516cc>, <twitter.User object at 0x825170c>, <twitter.User object at 0x825174c>, <twitter.User object at 0x825178c>, <twitter.User object at 0x82517cc>, <twitter.User object at 0x825180c>, <twitter.User object at 0x825184c>, <twitter.User object at 0x825188c>, <twitter.User object at 0x82518cc>, <twitter.User object at 0x825190c>, <twitter.User object at 0x825194c>]
[<twitter.User object at 0x82515cc>, <twitter.User object at 0x825160c>, <twitter.User object at 0x825164c>, <twitter.User object at 0x825168c>, <twitter.User object at 0x82516cc>, <twitter.User object at 0x825170c>, <twitter.User object at 0x825174c>, <twitter.User object at 0x825178c>, <twitter.User object at 0x82517cc>, <twitter.User object at 0x825180c>, <twitter.User object at 0x825184c>, <twitter.User object at 0x825188c>, <twitter.User object at 0x82518cc>, <twitter.User object at 0x825190c>, <twitter.User object at 0x825194c>]
[<twitter.User object at 0x82515cc>, <twitter.User object at 0x825160c>, <twitter.User object at 0x825164c>, <twitter.User object at 0x825168c>, <twitter.User object at 0x82516cc>, <twitter.User object at 0x825170c>, <twitter.User object at 0x825174c>, <twitter.User object at 0x825178c>, <twitter.User object at 0x82517cc>, <twitter.User object at 0x825180c>, <twitter.User object at 0x825184c>, <twitter.User object at 0x825188c>, <twitter.User object at 0x82518cc>, <twitter.User object at 0x825190c>, <twitter.User object at 0x825194c>]
[<twitter.User object at 0x82515cc>, <twitter.User object at 0x825160c>, <twitter.User object at 0x825164c>, <twitter.User object at 0x825168c>, <twitter.User object at 0x82516cc>, <twitter.User object at 0x825170c>, <twitter.User object at 0x825174c>, <twitter.User object at 0x825178c>, <twitter.User object at 0x82517cc>, <twitter.User object at 0x825180c>, <twitter.User object at 0x825184c>, <twitter.User object at 0x825188c>, <twitter.User object at 0x82518cc>, <twitter.User object at 0x825190c>, <twitter.User object at 0x825194c>]
[<twitter.User object at 0x82515cc>, <twitter.User object at 0x825160c>, <twitter.User object at 0x825164c>, <twitter.User object at 0x825168c>, <twitter.User object at 0x82516cc>, <twitter.User object at 0x825170c>, <twitter.User object at 0x825174c>, <twitter.User object at 0x825178c>, <twitter.User object at 0x82517cc>, <twitter.User object at 0x825180c>, <twitter.User object at 0x825184c>, <twitter.User object at 0x825188c>, <twitter.User object at 0x82518cc>, <twitter.User object at 0x825190c>, <twitter.User object at 0x825194c>]

The output of print username.screen_name is simply displays a list of that user's friends.

Here's how it works:
userlist is all the info on the user's friends
username is each entry in the list (it includes their avatar location, etc.)
screen_name is an element from that particular username

Edited 6 Years Ago by itsbrad212: n/a

As I suspected, userlist is a list of class User instances you are getting from Twitter, where username is the instance, something like <twitter.User object at 0x82515cc>, and username.screen_name is the name associated with that instance.

These names should show up if you apply the wx label correctly.

To test this out, add/replace this to your code in the proper place ...

# ...
text = ""
for username in userlist:
    print username.screen_name  # test
    text += username.screen_name + '\n'
wx.StaticText(panel, -1, text, (10,100))
# ...

Edited 6 Years Ago by vegaseat: code

As I suspected, userlist is a list of class User instances you are getting from Twitter, where username is the instance, something like <twitter.User object at 0x82515cc>, and username.screen_name is the name associated with that instance.

These names should show up if you apply the wx label correctly.

No luck. Is there something I should do to the code to make it work?

No luck. Is there something I should do to the code to make it work?

Yes, use the code in the above post.

# ...
text = ""
for username in userlist:
    print username.screen_name  # test
    text += username.screen_name + '\n'
wx.StaticText(panel, -1, text, (10,100))
# ...

Yes, use the code in the above post.

# ...
text = ""
for username in userlist:
    print username.screen_name  # test
    text += username.screen_name + '\n'
wx.StaticText(panel, -1, text, (10,100))
# ...

Oh sorry. My browser stopped loading and the code you typed didn't show up.
Anyway, I tried that and it printed out, just not in the GUI. It ended up printing it in the idle shell

Oh sorry. My browser stopped loading and the code you typed didn't show up.
Anyway, I tried that and it printed out, just not in the GUI. It ended up printing it in the idle shell

It should print in both console and Label

This question has already been answered. Start a new discussion instead.