2016-01-06 10 views
6

मैं urwid का उपयोग कर रहा हूं, जो ncurses में टर्मिनल उपयोगकर्ता इंटरफेस को डिजाइन करने के लिए एक पायथन "फ्रेमवर्क" है। हालांकि एक बात है कि मैं urwid में ऐसा करने में सक्षम नहीं हूं जो शाप में आसान था - कर्सर को अदृश्य बनाओ। जैसा कि अब है, कर्सर बटन चुनते समय दिखाई देता है, और यह सिर्फ सादे बदसूरत दिखता है। क्या इसे अक्षम करने का कोई तरीका है?Urwid: कर्सर अदृश्य बनाएं

उत्तर

2

urwidcurs_set फ़ंक्शन का उपयोग करता है, लेकिन इसे कहीं भी क्लास विधि के रूप में बेनकाब नहीं करता है। कोई इस विधि का उपयोग करने के लिए urwid को संशोधित कर सकता है; अन्यथा ऐसा करने का कोई विश्वसनीय तरीका नहीं है।

आप इसे issue के रूप में रिपोर्ट कर सकते हैं।

+1

मैं देखता हूं। मैंने वहां एक मुद्दा पोस्ट किया, उम्मीद है कि देवविद इस पर विचार करेंगे। – makos

+0

इस मुद्दे से लिंक करें: https://github.com/urwid/urwid/issues/170 –

2

मैं मानता हूं कि urwid.Button पर चमकती कर्सर थोड़ा लंगड़ा दिखता है, इसलिए मैं इसे छिपाने के लिए एक समाधान के साथ आया हूं। Urwid में, Button कक्षा WidgetWrap का एक उप-वर्ग है जिसमें SelectableIcon और दो टेक्स्ट विजेट (संलग्न "<" और ">" शामिल हैं)। यह SelectableIcon वर्ग है जो डिफ़ॉल्ट रूप से कर्सर की स्थिति को लेबल के पहले अक्षर पर सेट करता है। SelectableIcon उप-वर्गीकरण करके, कर्सर की स्थिति को संशोधित करके और फिर इसे urwid.WidgetWrap सबक्लास में लपेटकर आप अपना स्वयं का कस्टम बटन बना सकते हैं जो सभी चालें Button, या इससे भी अधिक कुछ कर सकता है।

यहां 'मेरे प्रोजेक्ट में यह कैसा दिखता है।

enter image description here

import urwid 

class ButtonLabel(urwid.SelectableIcon): 
    def __init__(self, text): 
     """ 
     Here's the trick: 
     we move the cursor out to the right of the label/text, so it doesn't show 
     """ 
     curs_pos = len(text) + 1 
     urwid.SelectableIcon.__init__(self, text, cursor_position=curs_pos) 

फिर, आपको एक WidgetWrap उपवर्ग है कि अपने कस्टम बटन वर्ग होगा में किसी भी अन्य वस्तुओं के साथ एक ButtonLabel वस्तु लपेट कर सकते हैं।

class FixedButton(urwid.WidgetWrap): 
    _selectable = True 
    signals = ["click"] 
    def __init__(self, label): 
     self.label = ButtonLabel(label) 
     # you could combine the ButtonLabel object with other widgets here 
     display_widget = self.label 
     urwid.WidgetWrap.__init__(self, urwid.AttrMap(display_widget, None, focus_map="button_reversed")) 

    def keypress(self, size, key): 
     """ 
     catch all the keys you want to handle here 
     and emit the click signal along with any data 
     """ 
     pass 

    def set_label(self, new_label): 
     # we can set the label at run time, if necessary 
     self.label.set_text(str(new_label)) 

    def mouse_event(self, size, event, button, col, row, focus): 
     """ 
     handle any mouse events here 
     and emit the click signal along with any data 
     """ 
     pass 

इस कोड में, वहाँ वास्तव में FixedButtonWidgetWrap उपवर्ग में विगेट्स के बहुत संयोजन नहीं है, लेकिन यदि आप एक LineBox में, एक "[" और "]" बटन के किनारों को जोड़ने के लपेट सकता है, इत्यादि। यदि यह सब अनिवार्य है, तो आप इवेंट हैंडलिंग फ़ंक्शंस को ButtonLabel कक्षा में ले जा सकते हैं, और जब इसे क्लिक किया जाता है तो इसे सिग्नल छोड़ दें।

बटन उलट जब उस पर उपयोगकर्ता चलता है, यह AttrMap में लपेट और कुछ पैलेट प्रविष्टि ("button_reversed", मेरे मामले में) करने के लिए focus_map सेट बनाने के लिए।

0

शराबी मास्टर जवाब की पंक्तियों के साथ, लेकिन "न्यूनतम इनवेसिव सर्जरी" के साथ:

class ButtonLabel(urwid.SelectableIcon): 
    ''' 
    use Drunken Master's trick to move the cursor out of view 
    ''' 
    def set_text(self, label): 
     ''' 
     set_text is invoked by Button.set_label 
     ''' 
     self.__super.set_text(label) 
     self._cursor_position = len(label) + 1 


class MyButton(urwid.Button): 
    ''' 
    - override __init__ to use our ButtonLabel instead of urwid.SelectableIcon 

    - make button_left and button_right plain strings and variable width - 
     any string, including an empty string, can be set and displayed 

    - otherwise, we leave Button behaviour unchanged 
    ''' 
    button_left = "[" 
    button_right = "]" 

    def __init__(self, label, on_press=None, user_data=None): 
     self._label = ButtonLabel("") 
     cols = urwid.Columns([ 
      ('fixed', len(self.button_left), urwid.Text(self.button_left)), 
      self._label, 
      ('fixed', len(self.button_right), urwid.Text(self.button_right))], 
      dividechars=1) 
     super(urwid.Button, self).__init__(cols) 

     if on_press: 
      urwid.connect_signal(self, 'click', on_press, user_data) 

     self.set_label(label) 

यहाँ, हम केवल बटन की उपस्थिति को संशोधित लेकिन अन्यथा अपने व्यवहार में कोई बदलाव नहीं छोड़ दें।

संबंधित मुद्दे