我需要帮助在 PYGTK 中选择和打印文本

发布于 2024-11-06 08:26:05 字数 609 浏览 3 评论 0原文

我正在尝试制作一个如下所示的程序。

Image

我该文件名为 Tux image.png 且我有另一个名为 File to use.txt 的文件,看起来像这样

Microsoft 苹果 惠普 戴尔 Linux Blackberry

我需要通过选择 Linux 一词并将其打印在图像下方,使程序看起来像上面的程序。这是我现在拥有的代码。

# 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 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 above 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栖迟 2024-11-13 08:26:05

我不确定您需要什么,但您可以像这样阅读 use.txt 文件:

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

'file_content' 现在应该包含:

Microsoft Apple HP Dell Linux Blackberry

您可以将它们拆分为列表:

choices = file_content.split()

之后您可以使用 gtk.ComboBox 来显示带有选项的组合框。

仍然不确定这是否是您想要的。

编辑:

添加组合和标签:

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()

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()
少女七分熟 2024-11-13 08:26:05

您有 2 个选择:
1-将图像文件加载到Pixbuff中,在其上绘制文本,并将其显示在窗口中,这是较难的方法。
2- 在窗口中添加一个gtk.Fixed,并添加gtk.Image(从文件加载)和gtk.Label(包括text)到该 gtk.Fixed 小部件,给定文本的特定位置(例如此处的 x=300,y=750)。像这样的东西:

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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文