2012-03-04 20 views
8

मैं जब विजेट फोकस नहीं है चयनित पाठ के लिए डिफ़ॉल्ट पृष्ठभूमि रंग मैक ओएस एक्स पर एक Tkinter पाठ विजेट में परिवर्तित करने के लिए कोशिश कर रहा हूँ पर चुनिंदा पृष्ठभूमि रंग बदल रहा है। डिफ़ॉल्ट फोकस चयनित रंग ग्रे है। खोज के कई घंटों के बाद, मैं ऐसा करने के लिए आउट ऑफ़ द बॉक्स समाधान ढूंढने में असमर्थ था। यहां मैंने कोशिश की है:Tkinter एक विकेन्द्रित पाठ विजेट

  • selectbackground विकल्प के साथ चयन रंग बदलना विजेट को केंद्रित नहीं होने पर चयन रंग नहीं बदलता है। जैसे यह भूरे रंग में रहता है।
  • ना ही Text.tag_configure("sel", background=...)
  • एंट्री विगेट्स (और अन्य) पर "!focus" राज्य कार्यों के साथ ttk.Style.map का उपयोग करना, लेकिन विगेट्स पाठ नहीं।

तो मुझे अपना खुद का रोल करना पड़ा (नीचे देखें)। क्या ऐसा करने के लिए इससे अच्छा तरीका है?

import Tkinter as tk 

# Replace 'tag_out' with 'tag_in' 
def replace_tag(widget, tag_out, tag_in): 
    ranges = widget.tag_ranges(tag_out) 
    widget.tag_remove(tag_out, ranges[0], ranges[1]) 
    widget.tag_add(tag_in, ranges[0], ranges[1]) 

def focusin(e): 
    replace_tag(e.widget, "sel_focusout", "sel") 

def focusout(e): 
    replace_tag(e.widget, "sel", "sel_focusout") 


root = tk.Tk() 

# Create a Text widget with a red selected text background 
text = tk.Text(root, selectbackground="red") 
text.pack() 

# Add some text, and select it 
text.insert("1.0", "Hello, world!") 
text.tag_add("sel", "1.0", "end") 

# Create a new tag to handle changing the background color on selected text 
# when the Text widget loses focus 
text.tag_configure("sel_focusout", background="green") 
replace_tag(text, "sel", "sel_focusout") 

# Bind the events to make this magic happen 
text.bind("<FocusIn>", focusin) 
text.bind("<FocusOut>", focusout) 


# Create an Entry widget to easily test the focus behavior 
entry = tk.Entry(root) 
entry.pack() 

entry.insert("0", "Focus me!") 

root.mainloop() 
+0

विंडोज़ की विपरीत समस्या है; डिफ़ॉल्ट रूप से, कोई निष्क्रिय चयन पृष्ठभूमि नहीं है। विंडोज़ पर भी ग्रे रहने के लिए, इडल के पास इस 'बग' के लिए एक कामकाज है जो आपके जैसा ही है। किसी ने हाल ही में 'inactiveselectbackground' की ओर इशारा किया है, इसलिए मैं आज कामकाज को हटा रहा हूं (मेरी इच्छा है कि मैंने एसओ खोजा था और एक साल पहले यह पाया था)। विकल्प https://www.tcl.tk/man/tcl8.5/TkCmd/text.htm#M-inactiveselectbackground पर प्रलेखित है। मैं अधूरे tkinter दस्तावेज़ों का विस्तार करने पर काम करने पर विचार कर रहा हूँ। –

उत्तर

11

रुपये स्रोत कोड के माध्यम से खुदाई मुझे जवाब में ले जाती है! inactiveselectbackground विकल्प रंग सेट करता है।

import Tkinter as tk 

root = tk.Tk() 

# Create a Text widget with a red selected text background 
# And green selected text background when not focused 
text = tk.Text(root, selectbackground="red", inactiveselectbackground="green") 
text.pack() 

# Add some text, and select it 
text.insert("1.0", "Hello, world!") 
text.tag_add("sel", "1.0", "end") 

# Create an Entry widget to easily test the focus behavior 
entry = tk.Entry(root) 
entry.pack() 

entry.insert("0", "Focus me!") 

root.mainloop()