开发者

I need help choosing and printing text in PYGTK

开发者 https://www.devze.com 2023-03-04 17:09 出处:网络
I am trying to make a program that looks like the one below. Image I that file is called Tux image.png and i have another called File to use.txt that looks like this

I am trying to make a program that looks like the one below.

Image

I that file is called Tux image.png and i have another called File to use.txt that looks like this

Microsoft Apple HP Dell Linux Blackberry

I need to make the program appear like the one ab开发者_如何学Pythonove by choosing the word Linux and printing it under the image.Here is the code that I have now.

# two underscores
Class tux:
    def __init__(self):
        win = gtk.Window( )
        img  = gtk.Image( )
        img.set_from_file(“Tux image.png”)
        win.add(img)
        win.show_all( )
        win.connect(‘destroy’,gtk.main_quit)


tux( )
gtk.main( )

I only need help with the importing of the document and printing the name at the bottom


I'm not sure what you need, but you can read the use.txt file like this:

fileobj = open("use.txt")
file_content = fileobj.read()

'file_content' should now contain:

Microsoft Apple HP Dell Linux Blackberry

You can split them in a list:

choices = file_content.split()

After that you could use gtk.ComboBox to display a combobox with choices.

Still not sure if this is what you want.

EDIT:

Adding a combo and a label:

class Tux(gtk.Window):
    def __init__(self):
        super(Tux, self).__init__()
        combobox = gtk.combo_box_new_text()
        combobox.connect("changed", self.on_changed)
        for choice in choices:
            combobox.append_text(choice)
        self.add(combobox)
        self.label = gtk.Label("No selection")
        self.add(self.label)
        img = gtk.Image( )
        img.set_from_file(“Tux image.png”)
        self.add(img)
        self.connect("destroy", gtk.main_quit)
        self.show_all()

def on_changed(self, widget):
    self.label.set_label(widget.get_active_text())


Tux()
gtk.main()


You have 2 options:
1- Load the image file into a Pixbuff, draw the text on it, and show it in the window, that is the hard way.
2- Add a gtk.Fixed into the window, and add both gtk.Image (loaded from file) and gtk.Label (including the text) to that gtk.Fixed widget, given specific location for text (for example x=300, y=750 here). Something like this:

class TuxWindow(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self)
        fixed = gtk.Fixed()
        ####
        image = gtk.Image()
        image.set_from_file('Tux image.png')
        fixed.put(image, 0, 0)
        ####
        text = open('use.txt').read()
        label = gtk.Label(text)
        fixed.put(label, 300, 750)
        self.add(fixed)
        fixed.show_all()
        ####
        self.connect('delete-event', lambda w, e: gtk.main_quit())

TuxWindow().present()
gtk.main()
0

精彩评论

暂无评论...
验证码 换一张
取 消