import twitter import re from javax swing import BoxLayout ImageIcon J

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import twitter
import re
from javax.swing import (BoxLayout, ImageIcon, JButton, JFrame, JPanel,
JPasswordField, JLabel, JTextArea, JTextField, JScrollPane,
SwingConstants, WindowConstants)
from java.awt import Component, GridLayout
from java.net import URL
from java.lang import Runnable
class JyTwitter(object):
def __init__(self):
self.frame = JFrame("Jython Twitter",
defaultCloseOperation = WindowConstants.EXIT_ON_CLOSE)
self.loginPanel = JPanel(GridLayout(0,2))
self.frame.add(self.loginPanel)
self.usernameField = JTextField('',15)
self.loginPanel.add(JLabel("username:", SwingConstants.RIGHT))
self.loginPanel.add(self.usernameField)
self.passwordField = JPasswordField('', 15)
self.loginPanel.add(JLabel("password:", SwingConstants.RIGHT))
self.loginPanel.add(self.passwordField)
self.loginButton = JButton('Log in',actionPerformed=self.login)
self.loginPanel.add(self.loginButton)
self.message = JLabel("Please Log in")
self.loginPanel.add(self.message)
self.frame.pack()
self.show()
def login(self,event):
self.message.text = "Attempting to Log in..."
username = self.usernameField.text
try:
self.api = twitter.Api(username, self.passwordField.text)
self.timeline(username)
self.loginPanel.visible = False
self.message.text = "Logged in"
except:
self.message.text = "Log in failed."
raise
self.frame.size = 400,800
def timeline(self, username):
timeline = self.api.GetFriendsTimeline(username)
self.resultPanel = JPanel()
self.resultPanel.layout = BoxLayout(self.resultPanel, BoxLayout.Y_AXIS)
for s in timeline:
self.showTweet(s)
scrollpane = JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)
scrollpane.preferredSize = 400, 800
scrollpane.viewport.view = self.resultPanel
self.frame.add(scrollpane)
def showTweet(self, status):
user = status.user
p = JPanel()
p.add(JLabel(ImageIcon(URL(user.profile_image_url))))
p.add(JTextArea(text = status.text,
editable = False,
wrapStyleWord = True,
lineWrap = True,
alignmentX = Component.LEFT_ALIGNMENT,
size = (300, 1)
))
self.resultPanel.add(p)
def show(self):
self.frame.visible = True
if __name__ == '__main__':
JyTwitter()