打开时,我的Tkinter GUI似乎在视觉上无法聚焦 [英] My Tkinter GUI seems visually out of focus when opened

查看:131
本文介绍了打开时,我的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天全站免登陆