为什么我的 Tkinter GUI 在打开时视觉上失焦? [英] Why is my Tkinter GUI visually out of focus when opened?

查看:16
本文介绍了为什么我的 Tkinter GUI 在打开时视觉上失焦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打开 GUI 时,我可以输入它并执行一些操作,但是 OptionMenu 和 Button 小部件看起来好像 GUI 失焦了.一张图来说明我的意思:(看看下拉菜单和按钮)

When I open my GUI, I can type in it and do stuff, but the OptionMenu and Button widgets look as if the GUI is out of focus. A picture to demonstrate what I mean: (take a look at the dropdown menus and the buttons)

在我关注不同的应用程序然后再次单击我的 GUI 后,它具有正确的颜色,如果它处于焦点,则应该在那里.又是一张图,让我更清楚我的意思:

After I focus on a different app and then click on my GUI again, it has the right colors which should be there if it is in focus. Once again a picture so it is clearer what I mean:

所以我的问题是,有没有人知道为什么会发生这种情况以及我应该怎么做才能使 GUI 在我第一次打开时在视觉上也清晰可见?

So my question is, does anyone know why this is happening and what I should do so that the GUI is also visually in focus when I open it for the first time?

我知道上传所有这些代码是不好的做法,因为最好上传一个最小的可重现示例,但我认为每个小细节在这里都可能很重要,为什么会发生这种情况.因此,我决定上传更大的用于 GUI 的代码.

I know it is bad practice to upload all this code as it's better to upload a minimal reproducible example, but I figured every small detail could be important here as to why this is happening. Therefore, I decided to upload a larger piece of the code I'm using for the GUI.

from tkinter import *
from tkinter import messagebox
from tkinter import font as tkfont

root = Tk()
root.config(background='#009688')
root.title('Contractmaker')

# GUI stuff that takes care of the scrollbar
def on_configure(event):
    canvas.configure(scrollregion=canvas.bbox('all'))

def on_mousewheel(event):
    canvas.yview_scroll(int(event.delta), 'units')

# Create some fonts
bold_font = tkfont.Font(weight='bold')

# Create the actual GUI
canvas = Canvas(root, width=450, height=550)
canvas.config(background='#009688')
canvas.pack(side=RIGHT)

scrollbar = Scrollbar(root, command=canvas.yview)
# scrollbar.pack(side=RIGHT, fill='y')

canvas.configure(yscrollcommand=scrollbar.set)
canvas.bind('<Configure>', on_configure)
canvas.bind_all('<MouseWheel>', on_mousewheel)

frame = Frame(canvas)
frame.config(background='#009688')
canvas.create_window((0,0), window=frame)

labelNaamhuurder = Label(frame, text='Naam huurder', bg='#009688', font=bold_font).grid(row=0, column=0, sticky=W, padx=(30, 0), pady=(15, 0))
naamhuurderr = Entry(frame, textvariable=naamhuurder, relief=FLAT, highlightcolor='#9DCCFD')
naamhuurderr.grid(row=0, column=2, pady=(15, 0))

# All the other rows are the same as the one above so I decided to leave them out 

labelAdresapp = Label(frame, text='Adres appartement', bg='#009688', font=bold_font).grid(row=5, column=0, pady=(15, 0), sticky=W, padx=(30, 0))
appartementen = {'Slotlaan 73', 'Slotlaan 77', 'Albert Cuypstraat 22'}
adresapp.set('Slotlaan 73') # Default option
dropdownMenuhuur = OptionMenu(frame, adresapp, *appartementen)
dropdownMenuhuur.config(width=18)
dropdownMenuhuur.grid(row=5, column=2, pady=(15, 0))

labelTypekamer = Label(frame, text='Type kamer', bg='#009688', font=bold_font).grid(row=6, column=0, pady=(15, 0), sticky=W, padx=(30, 0))
typeKamers = {'Grote kamer', 'Kleine kamer', 'Grote kamer gedeeld'}
typekamer.set('Grote kamer') # Default option
dropdownMenutypekamer = OptionMenu(frame, typekamer, *typeKamers)
dropdownMenutypekamer.config(width=18)
dropdownMenutypekamer.grid(row=6, column=2, pady=(15, 0))

empty = Button(frame, text='Opnieuw', command=clear, font=bold_font)
empty.config(width=10, fg='#009688', borderwidth=0, relief=RAISED)
empty.configure(highlightbackground='#009688')
empty.grid(row=11, column=0, pady=(25, 0), padx=(80, 0))

converter = Button(frame, text='OK', command=contractupdater, font=bold_font)
converter.config(width=10, fg='#009688', borderwidth=2, relief=RAISED)
converter.configure(highlightbackground='#009688')
converter.grid(row=11, column=2, pady=(25, 0), padx=(0, 80))

root.mainloop()

有关更多背景信息:这基本上是这个问题.

For some more context: this is basically a follow-up question on this question.

推荐答案

我不确定这是否是解决此问题的正确方法,但它确实有效.因此,通过使用 root.update() 我能够解决这个问题,但这导致了另一个问题,即在开始时闪烁窗口更改大小,这也可以解决,

I'm not sure if this is the right fix for this issue but it works. So by using root.update() I was able to solve the issue, but this leads to another issue of flashing window changing sizes from default at the start which can also be solved with this,

...
root = tk.Tk()
root.wm_withdraw()  # Hide the window (unmapped)
root.update()       # Update the window when it is hidden
...


...
# Show the window back again just before the mainloop with 1ms delay.
root.after(1, root.deiconify) 
root.mainloop()

这应该可以在一开始就专注地解决您的问题,否则请告诉我.

This should solve your issue with focus at the start, let me know otherwise.

这篇关于为什么我的 Tkinter GUI 在打开时视觉上失焦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆