Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 文本不会改变其高度,即使在传播关闭时也是如此_Python_Python 3.x_Textbox_Tkinter - Fatal编程技术网

Python 文本不会改变其高度,即使在传播关闭时也是如此

Python 文本不会改变其高度,即使在传播关闭时也是如此,python,python-3.x,textbox,tkinter,Python,Python 3.x,Textbox,Tkinter,我正在尝试实现两个新的GUI子类:一个是可以动态滚动和调整大小的Frame,另一个是保持其父级宽度但将其高度调整为文本行数的Text框 框架似乎工作正常,但文本没有调整大小;第一次对其进行网格化时,其高度为1,仅在调整框架大小时(通过单击并拖动边缘)才会更新。我一直认为我的代码越来越好,但我不知道出了什么问题。似乎Text类没有正确更新其高度。在ResizedText.resize()中,当我config将容器高度设置为20时,它无法正确更新。。。在该语句前后,height为1,但cget[“h

我正在尝试实现两个新的GUI子类:一个是可以动态滚动和调整大小的
Frame
,另一个是保持其父级宽度但将其高度调整为文本行数的
Text

框架
似乎工作正常,但
文本
没有调整大小;第一次对其进行网格化时,其高度为1,仅在调整框架大小时(通过单击并拖动边缘)才会更新。我一直认为我的代码越来越好,但我不知道出了什么问题。似乎Text类没有正确更新其高度。在
ResizedText.resize()
中,当我
config
将容器高度设置为20时,它无法正确更新。。。在该语句前后,height为1,但
cget[“height”]
返回20。任何关于如何使此代码更好的帮助都将不胜感激。谢谢

import tkinter as tk

class ResizedText(tk.Text):
    def __init__(self, parent, *args, **kwargs):
        self.parent = parent
        self.container_container = tk.Frame(parent, bg="blue")
        self.container = tk.Frame(self.container_container, bg="red")
        super().__init__(self.container, *args, **kwargs)
        self.container_container.bind("<Configure>", self.resize)
        self.bind("<KeyRelease>", self.resize)
    def grid(self, *args, **kwargs):
        self.container_container.grid(*args, **kwargs)
        self.container.grid_propagate(False)
        self.container.grid(row=0, column=0, sticky="nsew")
        super().grid(row=0, column=0, sticky="nsew")
        #self.container.grid_columnconfigure(0, weight=1)
    def bind(self, *args, **kwargs):
        if "add" not in kwargs: kwargs["add"] = True
        super().bind(*args, **kwargs)
    def resize(self, event=None):
        self.container.config(width=self.container_container.winfo_width())
        if self.bbox("end-1c"):
            text_height = (self.bbox("end-1c")[1]+self.bbox("end-1c")[3])+4
            if text_height < 20: text_height = 20
        else: text_height = 20
        self.container.config(height=text_height)
        #after this statement, self.container.winfo_height = 1
        #BUT self.container.cget("height") = 20

class ScrollableFrame(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        self.parent = parent
        self.frame = tk.Frame(parent)
        self.frame.grid_rowconfigure(0, weight=1)
        self.frame.grid_columnconfigure(0, weight=1)
        yscroll = tk.Scrollbar(self.frame)
        yscroll.grid(row=0, column=1, sticky="ns")
        self.canvas = tk.Canvas(self.frame, yscrollcommand=yscroll.set, *args,
                **kwargs)
        self.canvas.grid(row=0, column=0, sticky="nsew",)
        yscroll.config(command=self.canvas.yview)
        super().__init__(self.canvas)
        self.grid_propagate(False)
        self.canvas.create_window((0,0), window=self, anchor="nw")
        self.frame.bind("<Configure>", self.resize_main)
        self.bind("<Configure>", self.resize_canvas)
    def grid(self, *args, **kwargs):
        self.frame.grid(*args, **kwargs)
    def resize_canvas(self, event):
        self.canvas.config(scrollregion=self.canvas.bbox("all"))
    def resize_main(self, event):
        self.config(width=event.width, height=self.bbox("all")[3])

GUI = tk.Tk()
GUI.grid_rowconfigure(0, weight=1)
GUI.grid_columnconfigure(0, weight=1)
f = ScrollableFrame(GUI, bg="brown")
f.grid(row=0, column=0, sticky="nsew")
f.grid_columnconfigure(0, weight=1)
t = ResizedText(f, bg="blue")
t.grid(row=1, column=0, sticky="nsew")
将tkinter作为tk导入
类ResizedText(tk.Text):
定义初始化(自、父、*args、**kwargs):
self.parent=parent
self.container\u container=tk.Frame(父级,bg=“蓝色”)
self.container=tk.Frame(self.container\u container,bg=“红色”)
super()
self.container\u container.bind(“,self.resize)
self.bind(“,self.resize)
def网格(自身、*args、**kwargs):
self.container\u container.grid(*args,**kwargs)
self.container.grid_传播(False)
self.container.grid(行=0,列=0,sticky=“nsew”)
super().grid(行=0,列=0,sticky=“nsew”)
#self.container.grid\u columnconfigure(0,权重=1)
def绑定(自身、*args、**kwargs):
如果kwargs中没有“添加”:kwargs[“添加”]=True
super().bind(*args,**kwargs)
def调整大小(自我,事件=无):
self.container.config(width=self.container\u container.winfo\u width())
如果self.bbox(“end-1c”):
文本高度=(self.bbox(“end-1c”)[1]+self.bbox(“end-1c”)[3])+4
如果文字高度<20:文字高度=20
其他:文字高度=20
self.container.config(高度=文本高度)
#在此语句之后,self.container.winfo_height=1
#但是self.container.cget(“高度”)=20
类ScrollableFrame(tk.Frame):
定义初始化(自、父、*args、**kwargs):
self.parent=parent
self.frame=tk.frame(父级)
self.frame.grid_rowconfigure(0,权重=1)
self.frame.grid\u columnconfigure(0,权重=1)
yscroll=tk.Scrollbar(self.frame)
yscroll.grid(行=0,列=1,sticky=“ns”)
self.canvas=tk.canvas(self.frame,yscrollcommand=yscroll.set,*args,
**kwargs)
self.canvas.grid(行=0,列=0,sticky=“nsew”,)
yscroll.config(命令=self.canvas.yview)
super()
self.grid_传播(False)
self.canvas.create_window((0,0),window=self,anchor=“nw”)
self.frame.bind(“,self.resize\u main)
self.bind(“,self.resize_画布)
def网格(自身、*args、**kwargs):
self.frame.grid(*args,**kwargs)
def RESIGH_画布(自身、事件):
self.canvas.config(scrollregion=self.canvas.bbox(“全部”))
def resize_主(自身、事件):
self.config(width=event.width,height=self.bbox(“全部”)[3])
GUI=tk.tk()
GUI.grid_rowconfigure(0,权重=1)
GUI.grid\u columnconfigure(0,权重=1)
f=可滚动框架(GUI,bg=“brown”)
f、 网格(行=0,列=0,sticky=“nsew”)
f、 网格配置(0,权重=1)
t=调整大小的文本(f,bg=“蓝色”)
t、 网格(行=1,列=0,sticky=“nsew”)

resize()
的底部尝试
self.update\u idletasks()
self.update()


update()
命令将实现您对小部件所做的所有更改,如果这些更改最初没有发生。

在更改文本小部件的配置后,您是否尝试过对其执行
update\u idletasks()