zeroDev
V2EX  ›  问与答

问一个 Python 中 Class 变量传参的问题

  •  1
     
  •   zeroDev · Aug 16, 2018 · 3162 views
    This topic created in 3014 days ago, the information mentioned may be changed or developed.

    import urwid

    class QuestionBox(input_text, urwid.Filler):
        def keypress(self, size, key):
            if key != 'enter':
                return super(QuestionBox, self).keypress(size, key)
            else:
                print(input_box_start)
                raise urwid.ExitMainLoop()

    input_box_start = urwid.Edit()
    fill = QuestionBox(input_box_start)
    loop = urwid.MainLoop(fill)
    loop.run()
    print(input_box_start)

    请问这样一个程序,如何才能吧 input_box_start 传出来
    谢谢各位:)

    Supplement 1  ·  Aug 16, 2018
    class QuestionBox(input_text, urwid.Filler):
    这里改成
    class QuestionBox(urwid.Filler):
    手误打错了
    7 replies  •  2018-08-17 01:21:57 +08:00
    Yourshell
    Yourshell
    Aug 16, 2018 via iPhone
    1
    没看懂问的什么,你是想打印 input_box_start 吗,实现__str__方法就行了
    zeroDev
    zeroDev
    OP
    Aug 16, 2018 via Android
    2
    @Yourshell
    就是怎么把类里面的参数传出来用
    ToT
    ToT
    Aug 16, 2018
    3
    input_box_start 这里是 urwid.Edit() obj 吧?那你 print 是不是应该是 print(input_text).

    传个 list 的 reference 进去, 或者建一个 property under QuestionBox
    Yourshell
    Yourshell
    Aug 16, 2018 via iPhone
    4
    zeroDev
    zeroDev
    OP
    Aug 16, 2018
    5
    @Yourshell 翻过了,不是库的问题。
    纠结了晚上,不是随意提问
    ToT
    ToT
    Aug 17, 2018
    7
    @zeroDev global keyword 感觉以后会有坑。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4219 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 07:34 · PVG 15:34 · LAX 23:34 · JFK 02:34
    ♥ Do have faith in what you're doing.