选择combobox值tkinter python时删除突出显示的颜色

发布于 2025-01-28 20:28:52 字数 2754 浏览 5 评论 0原文

当用户从组合框下拉列表中选择值时,是否可以选择删除颜色突出显示?我正在使用主题“蛤”,并尝试了其他带有不同想法的主题,但到目前为止没有运气。这是我要删除的图像:

”在此处输入图像描述

这是我的代码下面:

clean_shift_win = Tk()
clean_shift_win.title('JUSTT Shift Request')
clean_shift_win.geometry('300x200')
dir_path_main_bg = os.path.join(dir_path, r'login window bg.png')
picture = PhotoImage(file=dir_path_main_bg, master=clean_shift_win)
clean_shift_win.option_add('*TCombobox*Listbox.selectBackground', '#30D5C8') # change 
highlight color
clean_shift_win.option_add('*TCombobox*Listbox.selectForeground', 'white') # change text 
color


clean_shift_canvas = Canvas(clean_shift_win, width=300, height=200,
                        highlightbackground='#30D5C8', highlightcolor='#30D5C8')
clean_shift_canvas.pack(fill='both', expand=True)
clean_shift_canvas.create_image(0, 0, image=picture, anchor='nw')
clean_shift_canvas.create_text(150,80,fill='turquoise',font='calibri 13 bold',
                text='Which Day You Want\n To Delete Your Shift?')

day_chose_to_delete = tk.StringVar(value='Weekday')
day_chose_to_delete_list = ttk.Combobox(clean_shift_canvas, 
textvariable=day_chose_to_delete, state='readonly', cursor='hand1', justify='center')


day_chose_to_delete_list['value'] = week
day_chose_to_delete_list.config(font=('calibri', '13'), width=15) 
day_chose_to_delete_list.place(x=70, y=110, anchor='nw')

def delete_shift():
    MsgBox = tk.messagebox.askquestion('Shift Cancellation','Are You Sure You Want To Delete This Shift', icon = 'warning')
    if MsgBox == 'yes':
        clear_cells()
        clean_shift_win.destroy()
    else:
        pass

day_to_delete_btn = ttk.Button(clean_shift_canvas, text='Delete This Shift', 
cursor='hand2', command=delete_shift)
day_to_delete_btn.place(relx=0.32, rely=0.8, relwidth=0.35, relheight=0.15)


style = ttk.Style(clean_shift_win)
style.theme_use('clam')
style.map('TButton', foreground=[('!disabled', 'white'), ('active', 'white')],
      background=[('!disabled', '#550a8a'), ('active', '#550a8a')],
      bordercolor=[('!disabled', '#550a8a'), ('active', '#550a8a')],
      borderwidth=[('!disabled', 0), ('active', 0)])

style.map('TCombobox', fieldbackground=[('!disabled', 'white'), ('!pressed', 'white')],
      background=[('!disabled', '#30D5C8'), ('active', '#30D5C8')],
      bordercolor=[('!disabled', '#30D5C8'), ('active', '#30D5C8')],
      borderwidth=[('!disabled', 0), ('active', 0)])

clean_shift_win.resizable(False, False)
dir_path_justt_icon = os.path.join(dir_path, r'JUSTT Logo.ico')
clean_shift_win.iconbitmap(dir_path_justt_icon)
clean_shift_win.mainloop()

Is there an option to remove the color highlighting when the user chooses the value from the combo box drop down? I'm using the theme "clam" and tried other themes with different ideas but no luck so far. Here is an image of what I'm trying to remove:

enter image description here

Here is my code below:

clean_shift_win = Tk()
clean_shift_win.title('JUSTT Shift Request')
clean_shift_win.geometry('300x200')
dir_path_main_bg = os.path.join(dir_path, r'login window bg.png')
picture = PhotoImage(file=dir_path_main_bg, master=clean_shift_win)
clean_shift_win.option_add('*TCombobox*Listbox.selectBackground', '#30D5C8') # change 
highlight color
clean_shift_win.option_add('*TCombobox*Listbox.selectForeground', 'white') # change text 
color


clean_shift_canvas = Canvas(clean_shift_win, width=300, height=200,
                        highlightbackground='#30D5C8', highlightcolor='#30D5C8')
clean_shift_canvas.pack(fill='both', expand=True)
clean_shift_canvas.create_image(0, 0, image=picture, anchor='nw')
clean_shift_canvas.create_text(150,80,fill='turquoise',font='calibri 13 bold',
                text='Which Day You Want\n To Delete Your Shift?')

day_chose_to_delete = tk.StringVar(value='Weekday')
day_chose_to_delete_list = ttk.Combobox(clean_shift_canvas, 
textvariable=day_chose_to_delete, state='readonly', cursor='hand1', justify='center')


day_chose_to_delete_list['value'] = week
day_chose_to_delete_list.config(font=('calibri', '13'), width=15) 
day_chose_to_delete_list.place(x=70, y=110, anchor='nw')

def delete_shift():
    MsgBox = tk.messagebox.askquestion('Shift Cancellation','Are You Sure You Want To Delete This Shift', icon = 'warning')
    if MsgBox == 'yes':
        clear_cells()
        clean_shift_win.destroy()
    else:
        pass

day_to_delete_btn = ttk.Button(clean_shift_canvas, text='Delete This Shift', 
cursor='hand2', command=delete_shift)
day_to_delete_btn.place(relx=0.32, rely=0.8, relwidth=0.35, relheight=0.15)


style = ttk.Style(clean_shift_win)
style.theme_use('clam')
style.map('TButton', foreground=[('!disabled', 'white'), ('active', 'white')],
      background=[('!disabled', '#550a8a'), ('active', '#550a8a')],
      bordercolor=[('!disabled', '#550a8a'), ('active', '#550a8a')],
      borderwidth=[('!disabled', 0), ('active', 0)])

style.map('TCombobox', fieldbackground=[('!disabled', 'white'), ('!pressed', 'white')],
      background=[('!disabled', '#30D5C8'), ('active', '#30D5C8')],
      bordercolor=[('!disabled', '#30D5C8'), ('active', '#30D5C8')],
      borderwidth=[('!disabled', 0), ('active', 0)])

clean_shift_win.resizable(False, False)
dir_path_justt_icon = os.path.join(dir_path, r'JUSTT Logo.ico')
clean_shift_win.iconbitmap(dir_path_justt_icon)
clean_shift_win.mainloop()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

波浪屿的海角声 2025-02-04 20:28:52

因此,我发现当您尝试更改主题时,您如何插入配置元素很重要。这三行对我有用。

style = ttk.Style()
combostyle = ttk.Style()
combostyle.theme_create('combostyle', parent='clam', settings = {'TCombobox': 
                                                             {'configure': {'selectbackground': 'white',
                                                                            'fieldbackground': 'white',
                                                                            'background': '#30D5C8',
                                                                            'bordercolor':'#30D5C8',
                                                                           'selectforeground': 'black'}}})

style.theme_use('combostyle')

So I found out there is an importance of how you insert the config elements when you try to change the theme. Those three lines worked for me.

style = ttk.Style()
combostyle = ttk.Style()
combostyle.theme_create('combostyle', parent='clam', settings = {'TCombobox': 
                                                             {'configure': {'selectbackground': 'white',
                                                                            'fieldbackground': 'white',
                                                                            'background': '#30D5C8',
                                                                            'bordercolor':'#30D5C8',
                                                                           'selectforeground': 'black'}}})

style.theme_use('combostyle')
自在安然 2025-02-04 20:28:52

我知道您的问题已经回答,但我想最简单的方法是将以下命令添加到您的样式中:

style = ttk.Style()
style.configure('TCombobox', selectbackground=None, selectforeground=None)

I know your question is already answered but I guess the simplest way is to add to your style the following commands:

style = ttk.Style()
style.configure('TCombobox', selectbackground=None, selectforeground=None)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文