Använda flera klasser eller ej i Python?

Här diskuteras programmering och utveckling
Användarvisningsbild
HOLMEN
Fadder
Inlägg: 1724
Blev medlem: 29 mar 2006, 22:39
OS: Ubuntu
Utgåva: Vet inte/ingen utgåva passar
Ort: Göteborg
Kontakt:

Använda flera klasser eller ej i Python?

Inlägg av HOLMEN »

Halloj,

Jag håller på och skapar en lite applikation som ska ligga i indicator-appleten. Jag vill även att den ska ha en submeny, olika val och en About-dialog i en och samma fil. Tidigare när jag har kodat lite lätt i Python så har jag lagt upp olika saker i olika filer just för att jag inte vetat hur man gör just detta. Nu är det dags att lära sig!

Detta är skriptet som det ser ut nu:

Kod: Markera allt

#!/usr/bin/env python
# Joyce is simple application in the indicator-applet. It uses sixad for handling.

# Imports needed modules
import pygtk
pygtk.require('2.0')
import gtk
import appindicator

# Creates the class for the application
class JoyceAppIndicator:
	def __init__(self):
		self.ind = appindicator.Indicator ("example-simple-client", "indicator-messages", appindicator.CATEGORY_APPLICATION_STATUS)
		self.ind.set_status (appindicator.STATUS_ACTIVE)
		self.ind.set_attention_icon ("indicator-messages-new")
		self.ind.set_icon("qtsixa")

		# create a menu
		self.menu = gtk.Menu()

		# create items for the menu - labels, checkboxes, radio buttons and images are supported:
        
		item = gtk.MenuItem("Connect/Disconnect")
		item.show()
		self.menu.append(item)

		# Open Profile Creator
		creator = gtk.MenuItem("Profile Creator")
		creator.show()
		self.menu.append(creator)

		# Open About
		about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
		self.about.connect("clicked", self.show_about, None)
		about.show()
		self.menu.append(about)

		# Create Submenu
	#	submenu = gtk.Menu()
	#	profile = gtk.MenuItem("Profile (Submenu)")
	#	profile.submenu.show()
	#	self.submenu.append(profile)
        	#radio = gtk.RadioMenuItem(None, "Radio Menu Item")
		#radio.set_submenu(submenu)

		# Quit-item
		image = gtk.ImageMenuItem(gtk.STOCK_QUIT)
		image.connect("activate", self.quit)
		image.show()
		self.menu.append(image)
                    
		self.menu.show()

		self.ind.set_menu(self.menu)

	def quit(self, widget, data=None):
		gtk.main_quit()

	# About
	def show_about(self, widget, data):
		# Create AboutDialog object
		dialog = gtk.AboutDialog()

		# Add the application name to the dialog
		dialog.set_name('Joyce - Gamepad manager')

		# Set the application version
		dialog.set_version('0.1')

		# Pass a list of authors.
		dialog.set_authors(['Daniel Holm', 'Filipe Cohelo'])

		# Add a short comment about the application.
		dialog.set_comments('Indicator Applet for sixad')

		# Add license information.
		dialog.set_license('Distributed under the GPL license.\nhttp://www.changethis.coms')

		# Show the dialog
		dialog.run()

		# The destroy method must be called otherwise the 'Close' button will
		# not work.
		dialog.destroy()

	def hide_dialog(self, widget, data):
		widget.hide()

	def main():
		gtk.main()
		return 0

if __name__ == "__main__":
	indicator = JoyceAppIndicator()
	main()
Saken är ju den att jag är en rätt grov nybörjare på Python och är lite osäker. Borde indiactor-appleten och about-dialogen ligga i separata klasser eller kan de ligga i samma. Och hur kallar jag respektive beroende på vilket som är korrekt?

Och vad betyder den sista if-satsen. Behöver jag en sådan till för about-dialogen? Tex:

Kod: Markera allt

elif __another_name__ == "__another_name__":
	about = AboutDialogExample()
	about.main()
Sen behöver jag hjälp med submenyn, men det får bli en ny tråd när jag väl klurat ut detta.
Användarvisningsbild
HOLMEN
Fadder
Inlägg: 1724
Blev medlem: 29 mar 2006, 22:39
OS: Ubuntu
Utgåva: Vet inte/ingen utgåva passar
Ort: Göteborg
Kontakt:

Re: Använda flera klasser eller ej i Python?

Inlägg av HOLMEN »

Ska det kanske likna något såsom:

Kod: Markera allt

#!/usr/bin/env python
# Joyce is simple application in the indicator-applet. It uses sixad for handling.

# Imports needed modules
import pygtk
pygtk.require('2.0')
import gtk
import appindicator

# Creates the class for the application
class JoyceAppIndicator:
	def __init__(self):
		self.ind = appindicator.Indicator ("example-simple-client", "indicator-messages", appindicator.CATEGORY_APPLICATION_STATUS)
		self.ind.set_status (appindicator.STATUS_ACTIVE)
		self.ind.set_attention_icon ("indicator-messages-new")
		self.ind.set_icon("qtsixa")

		# create a menu
		self.menu = gtk.Menu()

		# create items for the menu - labels, checkboxes, radio buttons and images are supported:
        
		item = gtk.MenuItem("Connect/Disconnect")
		item.show()
		self.menu.append(item)

		# Open Profile Creator
		creator = gtk.MenuItem("Profile Creator")
		creator.show()
		self.menu.append(creator)

		# Open About
		about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
		self.about.connect("clicked", self.show_about, None)
		about.show()
		self.menu.append(about)

		# Create Submenu
	#	submenu = gtk.Menu()
	#	profile = gtk.MenuItem("Profile (Submenu)")
	#	profile.submenu.show()
	#	self.submenu.append(profile)
        	#radio = gtk.RadioMenuItem(None, "Radio Menu Item")
		#radio.set_submenu(submenu)

		# Quit-item
		image = gtk.ImageMenuItem(gtk.STOCK_QUIT)
		image.connect("activate", self.quit)
		image.show()
		self.menu.append(image)
                    
		self.menu.show()

		self.ind.set_menu(self.menu)

	def quit(self, widget, data=None):
		gtk.main_quit()

# About
class AboutDialog:
	def show_about(self, widget, data):
		# Create AboutDialog object
		dialog = gtk.AboutDialog()

		# Add the application name to the dialog
		dialog.set_name('Joyce - Gamepad manager')

		# Set the application version
		dialog.set_version('0.1')

		# Pass a list of authors.
		dialog.set_authors(['Daniel Holm', 'Filipe Cohelo'])

		# Add a short comment about the application.
		dialog.set_comments('Indicator Applet for sixad')

		# Add license information.
		dialog.set_license('Distributed under the GPL license.\nhttp://www.changethis.coms')

		# Show the dialog
		dialog.run()

		# The destroy method must be called otherwise the 'Close' button will
		# not work.
		dialog.destroy()

	def hide_dialog(self, widget, data):
		widget.hide()

	if __name__ == "__main__":
	    about = AboutDialog()
	    about.main()

	def main():
		gtk.main()
		return 0

	if __name__ == "__main__":
		indicator = JoyceAppIndicator()
		main()
Användarvisningsbild
HOLMEN
Fadder
Inlägg: 1724
Blev medlem: 29 mar 2006, 22:39
OS: Ubuntu
Utgåva: Vet inte/ingen utgåva passar
Ort: Göteborg
Kontakt:

Re: Använda flera klasser eller ej i Python?

Inlägg av HOLMEN »

Efter en natts sömn och lite klurande så rädde jag ut koden. Nu ska här bara fixas submenyer.

Kod: Markera allt

#!/usr/bin/env python
# Joyce is simple application in the indicator-applet. It uses sixad for handling.

# Imports needed modules
import pygtk
pygtk.require('2.0')
import gtk
import appindicator

# Creates the class for the application
class JoyceAppIndicator:
	def __init__(self):
		self.ind = appindicator.Indicator ("example-simple-client", "indicator-messages", appindicator.CATEGORY_APPLICATION_STATUS)
		self.ind.set_status (appindicator.STATUS_ACTIVE)
		self.ind.set_attention_icon ("indicator-messages-new")
		self.ind.set_icon("qtsixa")

		# create a menu
		self.menu = gtk.Menu()

		# create items for the menu - labels, checkboxes, radio buttons and images are supported:
        
		item = gtk.MenuItem("Connect/Disconnect")
		item.show()
		self.menu.append(item)

		# Open Profile Creator
		creator = gtk.MenuItem("Profile Creator")
		creator.show()
		self.menu.append(creator)

		# Open About
		about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
		about.connect("activate", self.show_about, None)
		about.show()
		self.menu.append(about)

		# Create Submenu
	#	submenu = gtk.Menu()
	#	profile = gtk.MenuItem("Profile (Submenu)")
	#	profile.submenu.show()
	#	self.submenu.append(profile)
        	#radio = gtk.RadioMenuItem(None, "Radio Menu Item")
		#radio.set_submenu(submenu)

		# Quit-item
		image = gtk.ImageMenuItem(gtk.STOCK_QUIT)
		image.connect("activate", self.quit)
		image.show()
		self.menu.append(image)
                    
		self.menu.show()

		self.ind.set_menu(self.menu)

	def quit(self, widget, data=None):
		gtk.main_quit()

# About
	def show_about(self, widget, data):
		# Create AboutDialog object
		dialog = gtk.AboutDialog()

		# Add the application name to the dialog
		dialog.set_name('Joyce')

		# Set the application version
		dialog.set_version('0.1')

		# Pass a list of authors.
		dialog.set_authors(['Daniel Holm', 'Filipe Cohelo'])

		# Add a short comment about the application.
		dialog.set_comments('Indicator Applet for sixad')

		# Add license information.
		dialog.set_license('Distributed under the GPL license.\nhttp://www.changethis.coms')

		# Show the dialog
		dialog.run()

		# The destroy method must be called otherwise the 'Close' button will
		# not work.
		dialog.destroy()

	def hide_dialog(self, widget, data):
		widget.hide()

def main():
	gtk.main()
	return 0

if __name__ == "__main__":
	indicator = JoyceAppIndicator()
	main()
Skriv svar

Återgå till "Programmering och webbdesign"