Urwid键盘触发了弹出窗口

时间:2017-12-03 19:49:16

标签: python console-application urwid

当用户按下H键时,我试图在我的urwid应用程序顶部显示帮助对话框,但我似乎无法让它消失。它显示没有问题。我错过了什么?我现在已经在这一整天的大部分时间都在这里了。

我已经看过几个描述实现这个的不同方法的例子。我玩过信号,但无济于事。我希望避免使用可见的按钮来获取帮助,并完全依赖键盘快捷键。

public function store(UploadRequest $request)
    {

        // Save new transaction in database
          $transaction = new Transaction;
        $transaction->description = $request->description;
        $transaction->amount = $request->amount;
        $input  = $request->date;
            $format = 'd/m/Y';
            $date = Carbon::createFromFormat($format, $input);

        $transaction->date = $date;
        $transaction->transactiontype_id = $request->transactiontype;
        $transaction->user_id = Auth::id();
        $transaction->save();

        // Put tags in array
        $inputtags = explode(",", $request->tags);

        // Loop through every tag exists
        // EXISTS: get ID
        // NOT EXISTS: Create and get ID
        foreach ($inputtags as $inputtag)
        {
            $tag = Tag::firstOrCreate(['description' => $inputtag]);
            $transaction->tags()->attach($tag->id); //Put the 2 ID's in intermediate table ('tag_transaction')
        }


        //Check if there are files 
        if (!is_null($request->attachments))
        {
            //Loop through every file and upload it
            foreach ($request->attachments as $attachment) {
            $filename = $attachment->store('attachments');

            // Store the filename in the database with link to the transaction
            Attachment::create([
                'transaction_id' => $transaction->id,
                'path' => $filename
            ]);
            }
        }

1 个答案:

答案 0 :(得分:2)

你能想象在我意识到我需要做的就是把按钮的回调放在括号的回调上后我觉得多么愚蠢吗?

footer = urwid.Button('Okay', self.reset_layout)

这里有一些示例代码,可供将来偶然发现的人使用。

import urwid

class Application(object):
    '''
    The console UI
    '''

    # The default color palette
    _palette = [
        ('banner', 'black', 'light gray'),
        ('selectable', 'white', 'black'),
        ('focus', 'black', 'light gray')
    ]

    def __init__(self):
        self._body = urwid.SolidFill('.')
        self._pile = urwid.Pile(
            [
                self.dialog()
            ]
        )
        self._over = urwid.Overlay(
            self._pile,
            self._body,
            align = 'center',
            valign = 'middle',
            width = 20,
            height = 10
        )

        # Loop
        self._loop = urwid.MainLoop(
            self._over,
            self._palette,
            unhandled_input = self._handle_input
        )

    def _handle_input(self, key):
        '''
        Handles user input to the console UI

        Args:
            key (object): A mouse or keyboard input sequence
        '''

        if type(key) == str:
            if key in ('q', 'Q'):
                raise urwid.ExitMainLoop()
        elif type(key) == tuple:
            pass

    def start(self):
        '''
        Starts the console UI
        '''

        self._loop.run()

    def do(self, thing):
        self._loop.widget = self._body
        #self._pile.contents.clear()

    def dialog(self):
        '''
        Overlays a dialog box on top of the console UI
        '''

        # Header
        header_text = urwid.Text(('banner', 'Help'), align = 'center')
        header = urwid.AttrMap(header_text, 'banner')

        # Body
        body_text = urwid.Text('Hello world', align = 'center')
        body_filler = urwid.Filler(body_text, valign = 'top')
        body_padding = urwid.Padding(
            body_filler,
            left = 1,
            right = 1
        )
        body = urwid.LineBox(body_padding)

        # Footer
        footer = urwid.Button('Okay', self.do)
        footer = urwid.AttrWrap(footer, 'selectable', 'focus')
        footer = urwid.GridFlow([footer], 8, 1, 1, 'center')

        # Layout
        layout = urwid.Frame(
            body,
            header = header,
            footer = footer,
            focus_part = 'footer'
        )

        return layout

Application().start()