Python 如何在调整画布/框架大小时防止窗口闪烁?
下面看到的代码在技术上是功能性的,能够在画布中的一个框架中显示一组测试标签,然后用于滚动项目 我的问题是,当我运行代码并尝试在值之间滚动时,窗口(不确定哪个窗口)将重复闪烁,因为其中的画布或框架/标签试图调整自身大小 这段代码是我正在开发的一个更大的程序的一部分,但我已经去掉了不需要在那里进行故障排除的所有内容 我的测试方法Python 如何在调整画布/框架大小时防止窗口闪烁?,python,python-3.x,tkinter,tkinter-canvas,Python,Python 3.x,Tkinter,Tkinter Canvas,下面看到的代码在技术上是功能性的,能够在画布中的一个框架中显示一组测试标签,然后用于滚动项目 我的问题是,当我运行代码并尝试在值之间滚动时,窗口(不确定哪个窗口)将重复闪烁,因为其中的画布或框架/标签试图调整自身大小 这段代码是我正在开发的一个更大的程序的一部分,但我已经去掉了不需要在那里进行故障排除的所有内容 我的测试方法 运行代码并在文本框中输入随机字符串,然后按search 通过向下拖动来调整窗口大小,以便于滚动 注意到窗口闪烁的问题,画布想要调整自身大小。在一些滚动之后,画布将扩展到它
我不是使用
tkinter
领域的专家,但根据我的测试,我所能想到的是,不知何故,我限制了画布的大小,而不是限制其中的框架或标签,从而导致了它们之间的某种问题。如果不是这样的话,有一件事我知道,画布不喜欢它的初始尺寸,并且出于某种原因确实想要改变。我想我只是看了太久的代码,这个问题可能已经隐藏在普通的视野中,我需要另一双眼睛来让我明白
from tkinter import *
# colors and other variables
color_dark_grey = "#2C2C2C" # background color
color_light_grey = "#424242" # frame colors
pad_outer = 10
fontsize = 12
results_thickness = 100
# main window stuff
window = Tk() # creates the window
window.title("My Engineering Glossary")
window.configure(background=color_dark_grey)
# Puts stuff into the search results area to test it
def print_search():
for i in range(0, 20):
Label(frm_results_inner, text="col1 text", bg="grey").grid(row=i, column=1)
Label(frm_results_inner, text="col 2, longer text", bg="magenta").grid(row=i, column=2)
Label(frm_results_inner, text="col 3, a little longer still", bg="blue").grid(row=i, column=3)
# is purely to get the scrollbar to work. Leave it alone
def scroll_results_event(event): # for search results
canv_results.configure(scrollregion=canv_results.bbox("all"))
# will check what has been inputted into the text box to determine what to print
def check_input():
txt = txt_search.get()
if txt == "": # when there is nothing in the text box
frm_results.pack_forget() # hides the canvas with search results
lab_results.pack(padx=pad_outer / 2, pady=pad_outer / 4) # shows the no search frame
else: # when there is something in the text box
lab_results.pack_forget() # hides the frame for no search
# packs in the scrolbar, canvas, and wrapping frame
frm_results.pack(fill="both", expand=True)
# calls function to print out results
print_search()
# ============================================ NOW THE MAIN CODE BODY BEGINS ===========================================
# ---------------------------area for text input to search stuff---------------------------
# the frame for the accompanying stuff to go in
frm_input = Frame(window, bg=color_light_grey)
frm_input.pack(pady=pad_outer, fill="both")
# lable to indicate search region
lab_search = Label(frm_input, text="What do you want to search for?", bg=color_light_grey, fg='white',
font=("", fontsize))
lab_search.pack(padx=pad_outer / 2, pady=pad_outer / 4)
# text box to get user input
txt_search = Entry(frm_input, bg=color_light_grey, fg="white")
txt_search.pack(fill="both", expand=True, side="left", padx=pad_outer / 4, pady=pad_outer / 4)
# button to begin searching. Calls the "check_input" function that starts the process of printing results
btn_search = Button(frm_input, text="Search", bg=color_light_grey, fg="white", activebackground=color_dark_grey,
activeforeground="white", command=check_input)
btn_search.pack(side="right", padx=pad_outer / 4, pady=pad_outer / 6)
# ---------------------------area for search results---------------------------
# wrapper frame for everything going into the search results area
frm_results = Frame(window, bg=color_light_grey)
frm_results.pack(fill="both", expand=True, ipadx=results_thickness)
# lable to indicate nothing has been searched. will be packed in with the check_input() function
lab_results = Label(frm_results, text="Nothing has been searched.",
bg=color_light_grey, fg='white', font=("", fontsize))
# the outer frame that will hold the actual list of results
frm_results = Frame(frm_results, bg=color_light_grey)
# lable indicating search results are ready
lbl_canv_results = Label(frm_results, text="Test Search results", bg=color_light_grey, fg='white', font=("", fontsize))
# the canvas that will enable the possibility to scroll through the various search results
canv_results = Canvas(frm_results, bg=color_light_grey)
# frame in which the results will be listed
frm_results_inner = Frame(canv_results, bg=color_light_grey)
# scroll bar that will can scroll through results shown in frm_results_inner on the canvas
srlb_results = Scrollbar(canv_results, orient="vertical", command=canv_results.yview)
# configures the canvas to include a scrolling command linked to the scrollbar
canv_results.configure(yscrollcommand=srlb_results.set)
# pack in everything for the search results. They won't show up because
lbl_canv_results.pack(padx=pad_outer / 2, pady=pad_outer / 4)
srlb_results.pack(side="right", fill="y")
canv_results.pack(fill="both", expand=True, padx=pad_outer / 2, pady=pad_outer / 4)
# creates a window in which the frame is placed. This allows the frame to be scrolled through
canv_results.create_window((1, 1), window=frm_results_inner, anchor='nw')
# calls the function that will actually enable the scrolling. I don't understand why this works so leave it alone
frm_results_inner.bind("<Configure>", scroll_results_event)
check_input() # will look at input in the text box to display first item
window.mainloop() # keeps the window open
从tkinter导入*
#颜色和其他变量
颜色为深灰色=“#2C2C2C”#背景色
颜色为浅灰色=“#424242”#边框颜色
外垫=10
字体大小=12
结果_厚度=100
#主窗口材料
window=Tk()#创建窗口
窗口标题(“我的工程术语表”)
配置(背景=颜色\深\灰色)
#将内容放入搜索结果区域进行测试
def print_search():
对于范围(0,20)内的i:
标签(frm_结果_内部,text=“col1 text”,bg=“grey”)。网格(行=i,列=1)
标签(frm_结果_内部,text=“col 2,longer text”,bg=“magenta”)。网格(行=i,列=2)
标签(frm_results_inner,text=“col 3,再长一点”,bg=“blue”).grid(行=i,列=3)
#只是为了让滚动条正常工作。别管它
def scroll_results_event(事件):#搜索结果
canv_results.configure(scrollregion=canv_results.bbox(“全部”))
#将检查文本框中输入的内容,以确定要打印的内容
def check_input():
txt=txt_search.get()
如果txt==“”:#当文本框中没有任何内容时
frm_results.pack_forget()#隐藏包含搜索结果的画布
lab_results.pack(padx=pad_outer/2,pady=pad_outer/4)#显示无搜索框
else:#当文本框中有内容时
lab_results.pack_forget()#隐藏框架,不进行搜索
#包装在阴囊、帆布和包装架中
frm_results.pack(fill=“both”,expand=True)
#调用函数以打印结果
打印搜索()
#==========================================================================================现在主代码主体开始===========================================
#------------------------------用于搜索内容的文本输入区域---------------------------
#随附物品进入的框架
frm\u输入=帧(窗口,背景=颜色\u浅灰色)
frm_input.pack(pady=pad_outer,fill=“两者”)
#用于指示搜索区域的标签
lab\u search=Label(frm\u输入,text=“您想要搜索什么?”,bg=color\u light\u grey,fg='white',
字体=(“”,fontsize))
lab_search.pack(padx=pad_outer/2,pady=pad_outer/4)
#获取用户输入的文本框
txt\u搜索=输入(frm\u输入,背景=颜色\u浅灰色,fg=“白色”)
txt_search.pack(fill=“both”,expand=True,side=“left”,padx=pad_outer/4,pady=pad_outer/4)
#按钮开始搜索。调用“check_input”函数,该函数启动打印结果的过程
btn_search=按钮(frm_输入,text=“search”,bg=color\u light\u grey,fg=“white”,activebackground=color\u dark\u grey,
activeforeground=“白色”,命令=检查输入)
btn_search.pack(side=“right”,padx=pad_outer/4,pady=pad_outer/6)
#------------------------------搜索结果区域---------------------------
#搜索结果区域中所有内容的包装框架
frm\u结果=帧(窗口,背景=颜色\u浅灰色)
frm_results.pack(fill=“both”,expand=True,ipadx=results_thickness)
#指示未搜索任何内容的标签。将与check_input()函数一起打包
实验室结果=标签(frm\U结果,text=“未搜索任何内容。”,
bg=颜色\浅灰色,fg='白色',字体=(“”,fontsize))
#保存实际结果列表的外部框架
frm_结果=帧(frm_结果,背景=颜色_浅灰色)
#指示搜索结果已准备就绪的标签
lbl_canv_results=标签(frm_results,text=“测试搜索结果”,bg=color_light_grey,fg='white',font=(“”,fontsize))
#画布可以滚动浏览各种搜索结果
canv_结果=画布(frm_结果,bg=颜色_浅灰色)
#将在其中列出结果的框架
frm\U结果\U内部=帧(canv\U结果,背景=颜色\U浅灰色)
#滚动条,可滚动浏览画布上frm_results_inner中显示的结果
srlb_结果