2012-03-04 32 views
8

Tôi cố gắng để thay đổi màu nền mặc định cho văn bản đã chọn trong một Text Widget Tkinter trên Mac OS X khi widget không có trọng tâm. Màu chọn không tập trung mặc định là màu xám. Sau nhiều giờ tìm kiếm, tôi không thể tìm ra giải pháp ngoài hộp để thực hiện việc này. Dưới đây là những gì tôi đã thử:Tkinter thay đổi màu nền chọn trên một widget chữ không tập trung

  • Thay đổi màu chọn bằng tùy chọn selectbackground không thay đổi màu được chọn khi tiện ích không được tập trung. Ví dụ. Nó vẫn xám.
  • Cũng không Text.tag_configure("sel", background=...)
  • Sử dụng ttk.Style.map với trạng thái hoạt động trên tiện ích con nhập và các tiện ích văn bản khác.

Vì vậy, tôi phải tự cuộn (xem bên dưới). Có cách nào tốt hơn để làm điều này?

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

Windows có vấn đề ngược lại; theo mặc định, không có nền lựa chọn không hoạt động. Để làm cho nó ở lại màu xám trên Windows cũng có, Idle có một workaround cho 'lỗi' này là tương tự như của bạn. Một người nào đó gần đây đã chỉ ra 'không hoạt động chọn lại', vì vậy tôi đang loại bỏ cách giải quyết ngày hôm nay (tôi ước gì tôi đã tìm kiếm SO và tìm thấy điều này một năm trước). Tùy chọn được ghi lại tại https://www.tcl.tk/man/tcl8.5/TkCmd/text.htm#M-inactiveselectbackground. Tôi đang xem xét làm việc để mở rộng tài liệu tkinter chưa hoàn chỉnh. –

Trả lời

11

Đào qua mã nguồn Tk dẫn tôi đến câu trả lời! Tùy chọn inactiveselectbackground đặt màu.

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() 
Các vấn đề liên quan