You are not logged in.

  • Login

1

Saturday, March 27th 2010, 3:19pm

Beispiel für PyGTK-Twitter anwendung

Ich habe mal eine Beispiel Anwendung zur Benutzung des python-twitter modul durch das PyGTK modul geschrieben.

Weitere infos zu pygtk findet ihr unter http://pygtk.org/ , zum python-twitter modul unter http://code.google.com/p/python-twitter/ .
main.py datei:

Python Quellcode

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
#!/usr/bin/python
#-*- coding: utf-8 -*-
import pygtk #import the needed gtk modules
import gtk
import gtk.glade #import the files to include a glade-file
import twitter #import twitter modules. visit http://bit.ly/fgLs5 to install it
 
class client(object): #
	def run(self):
    	try:
        	self.wndwMain.show() #show the mainwindow
        	gtk.main() #start the eventloop
 
    	except KeyboardInterrupt: #if there's an error then pass
        	pass
 
	def __init__(self): #a function, which is automatically called, when the class is used
    	self.builder = gtk.Builder() #create builder to load glade file
    	self.builder.add_from_file("main.glade") #load glade file "main.glade"
    	self.builder.connect_signals(self) #connect the signals, which are created in glade
 
#you can edit the signals of every widgets in glade under properties -> Signals
 
    	#get all the widgets included in the glade file, that we need to talk to later
#how i name the widgets: (short version of widget: Window ~ wndw, Entry ~ ent) + (the using of it: Button for Tweet a meesage: (btn) Tweet)
#i write the second part of the name with an capitalized first char. That way its easier to read. Of course you can give them other names
    	self.wndwMain = self.builder.get_object("wndwMain")
    	self.btnTweet = self.builder.get_object("btnTweet")
    	self.entTweet = self.builder.get_object("entTweet")
    	self.lblChars = self.builder.get_object("lblChars")
    	self.btnLog = self.builder.get_object("btnLog")
    	self.entUser = self.builder.get_object("entUser")
    	self.entPass = self.builder.get_object("entPass")
 
	def on_wndwMain_destroy(self, *args):
    	gtk.main_quit() #end the program when the user hits the "X". You need to include that in PyGTK
 
	def on_entTweet_changed(self, *args): #show the user how many chars are left
    	self.lblChars.set_text("Chars left: " + str(140 - self.entTweet.get_text_length())) #twitter gives you 140 chars for one message
 
	def on_btnLog_clicked(self, *args): #log the user in
    	self.twit = twitter.Api(username = self.entUser.get_text(), password = self.entPass.get_text()) #i name my Api "twit". set username and password to the value of the entries
 
	def on_btnTweet_clicked(self, *args): #send a tweet
    	self.twit.PostUpdate(self.entTweet.get_text()) #get the value of the entTweet and post it to Twitter
 
if __name__ == "__main__":
	app = client()
	app.run()


main.glade datei:

XML Code

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?xml version="1.0"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="wndwMain">
	<property name="default_width">300</property>
	<property name="default_height">150</property>
	<signal name="destroy" handler="on_wndwMain_destroy"/>
	<child>
  	<object class="GtkVBox" id="boxMain">
    	<property name="visible">True</property>
    	<child>
      	<object class="GtkVBox" id="vbox1">
        	<property name="visible">True</property>
        	<child>
          	<object class="GtkHButtonBox" id="hbuttonbox3">
            	<property name="visible">True</property>
            	<child>
              	<object class="GtkLabel" id="lblUser">
                	<property name="visible">True</property>
                	<property name="label" translatable="yes">Username:</property>
              	</object>
              	<packing>
                	<property name="expand">False</property>
                	<property name="fill">False</property>
                	<property name="position">0</property>
              	</packing>
            	</child>
            	<child>
              	<object class="GtkEntry" id="entUser">
                	<property name="visible">True</property>
                	<property name="can_focus">True</property>
                	<property name="invisible_char">&#x25CF;</property>
              	</object>
              	<packing>
                	<property name="position">1</property>
              	</packing>
            	</child>
          	</object>
          	<packing>
            	<property name="expand">False</property>
            	<property name="fill">False</property>
            	<property name="position">0</property>
          	</packing>
        	</child>
        	<child>
          	<object class="GtkHButtonBox" id="hbuttonbox2">
            	<property name="visible">True</property>
            	<child>
              	<object class="GtkLabel" id="lblPass">
                	<property name="visible">True</property>
                	<property name="label" translatable="yes">Password:</property>
              	</object>
              	<packing>
                	<property name="expand">False</property>
                	<property name="fill">False</property>
                	<property name="position">0</property>
              	</packing>
            	</child>
            	<child>
              	<object class="GtkEntry" id="entPass">
                	<property name="visible">True</property>
                	<property name="can_focus">True</property>
                	<property name="invisible_char">&#x25CF;</property>
              	</object>
              	<packing>
                	<property name="expand">False</property>
                	<property name="fill">False</property>
                	<property name="position">1</property>
              	</packing>
            	</child>
          	</object>
          	<packing>
            	<property name="expand">False</property>
            	<property name="fill">False</property>
            	<property name="padding">11</property>
            	<property name="position">1</property>
          	</packing>
        	</child>
        	<child>
          	<object class="GtkHButtonBox" id="hbuttonbox1">
            	<property name="visible">True</property>
            	<property name="layout_style">end</property>
            	<child>
              	<object class="GtkButton" id="btnLog">
                	<property name="label" translatable="yes">Log in</property>
                	<property name="visible">True</property>
                	<property name="can_focus">True</property>
                	<property name="receives_default">True</property>
              	</object>
              	<packing>
                	<property name="expand">False</property>
                	<property name="fill">False</property>
                	<property name="position">0</property>
              	</packing>
            	</child>
          	</object>
          	<packing>
            	<property name="position">2</property>
          	</packing>
        	</child>
      	</object>
      	<packing>
        	<property name="position">0</property>
      	</packing>
    	</child>
    	<child>
      	<object class="GtkHButtonBox" id="btnboxTweet">
        	<property name="visible">True</property>
        	<property name="layout_style">center</property>
        	<child>
          	<object class="GtkLabel" id="lblChars">
            	<property name="visible">True</property>
            	<property name="label" translatable="yes">Chars left: 140</property>
          	</object>
          	<packing>
            	<property name="expand">False</property>
            	<property name="fill">False</property>
            	<property name="position">0</property>
          	</packing>
        	</child>
        	<child>
          	<object class="GtkEntry" id="entTweet">
            	<property name="visible">True</property>
            	<property name="can_focus">True</property>
            	<property name="invisible_char">&#x25CF;</property>
            	<signal name="changed" handler="on_entTweet_changed"/>
          	</object>
          	<packing>
            	<property name="expand">False</property>
            	<property name="fill">False</property>
            	<property name="position">1</property>
          	</packing>
        	</child>
        	<child>
          	<object class="GtkButton" id="btnTweet">
            	<property name="label" translatable="yes">Tweet</property>
            	<property name="visible">True</property>
            	<property name="can_focus">True</property>
            	<property name="receives_default">True</property>
            	<signal name="clicked" handler="on_btnTweet_clicked"/>
          	</object>
          	<packing>
            	<property name="expand">False</property>
            	<property name="fill">False</property>
            	<property name="position">2</property>
          	</packing>
        	</child>
      	</object>
      	<packing>
        	<property name="expand">False</property>
        	<property name="fill">False</property>
        	<property name="position">1</property>
      	</packing>
    	</child>
  	</object>
	</child>
  </object>
</interface>

This post has been edited 1 times, last edit by "malontop" (Mar 29th 2010, 8:23am)


2

Sunday, March 28th 2010, 4:46pm

Sehr schön, hab immer nach Beispielen PyGTK gesucht. Das ist mal ein nettes.

Frage dazu:

Wie sieht die Oberfläche in Vista, Windows 7 und Mac OS aus? Ich hab leider noch keine Beispiele gefunden, weiß aber, dass die Sache eigentlich für Gnome entwickelt wurde. Merkt man das?

3

Sunday, March 28th 2010, 7:27pm

Die Oberfläche sollte nativ aussehen. GIMP sieht auch unter Windows gut aus. Aber du musst auf jeden Fall auf dem Zielrechner GTK installiert haben. Python ja auch. Meiner Meinung nach, beziehungsweise dem nach, was ich meine, einmal gelesen zu haben, zeichnet GTK die widgets selbst. D.h. es sieht unter Umständen nativ aus, ist es aber gar nicht. Besser wäre die Benutzung von wxWidgets. wxWidgets benutzt die native GUI API.

Similar threads

Social bookmarks