首页 > 解决方案 > Python3:无法获取 Gtk.SpinButton 值

问题描述

我正在尝试在Gtk3+. 但是,我不知道如何调用我的Gtk.SpinButton值来驱动稍后在代码中执行的操作数量(尚未编写)。

initiate()代码末尾的类是我将根据输入的数字执行命令的地方gtk.spinbutton。现在,我留下了一个简单的打印命令,这样我就可以知道我的代码何时正常工作。

抱歉,如果我共享太多代码,我不知道如何在不共享所有代码的情况下正确显示我的问题。

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
import threading

class MainWindow(Gtk.Window):

        def __init__(self):
                Gtk.Window.__init__(self, title="My App")
                self.set_default_size(100, 100)
                self.set_border_width(10)

                #box
                self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=100)
                self.add(self.box)

                #label
                label = Gtk.Label()
                label.set_text("Welcome to My App!")
                self.box.pack_start(label, True, True, 0)

                #Number of files too rename
                self.renamenum = Gtk.SpinButton()
                self.box.pack_start(self.renamenum, True, True, 0)
                numfiles = self.renamenum.get_value();

                #Button1
                self.button1 = Gtk.Button(label="Start")
                self.button1.connect("clicked", self.button1_clicked)
                self.box.pack_end(self.button1, True, True, 0)

        def button1_clicked(self, widget):
                dialog = PopUp(self)
                response = dialog.run()
                if response == Gtk.ResponseType.CANCEL:
                        dialog.destroy()
                        Gtk.main_quit()
                else:
                        pass

class PopUp(Gtk.Dialog):

        def __init__(self, parent):
                Gtk.Dialog.__init__(self, "Renaming Files", parent, 
Gtk.DialogFlags.MODAL, (
                        "Cancel", Gtk.ResponseType.CANCEL
                ))
                self.set_default_size(500, 500)
                self.set_border_width(50)

                area = self.get_content_area()
                area.add(Gtk.Label("Running..."))
                self.show_all()

                initiate()

class initiate(MainWindow):

        def __init__(self):
                self.var1 = MainWindow()
                num = int(self.var1.numfiles)
                print (numfiles)

window = MainWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()

我想要的预期结果是输入的数字gtk.spinbutton,也可以在终端打印。相反,我得到了这个:

Traceback (most recent call last):
  File "test4.py", line 33, in button1_clicked
    dialog = PopUp(self)
  File "test4.py", line 54, in __init__
    initiate()
  File "test4.py", line 60, in __init__
    num = int(self.var1.numfiles)
AttributeError: 'MainWindow' object has no attribute 'numfiles'

标签: pythonpython-3.xgtk3

解决方案


推荐阅读