上一个动态创建的行不可见

时间:2019-11-30 12:16:23

标签: python-3.x tkinter scrollbar

代码如下:

from tkinter import *

class Window(Canvas):
    def __init__(self,master=None,**kwargs):
        Canvas.__init__(self,master,**kwargs)
        self.frame = Frame(self)
        self.create_window(0,0,anchor=N+W,window=self.frame)
        self.row = 1

        self.input_x = []
        self.input_y = []

        self.x_values = []
        self.y_values = []

        self._init_entries()

    def _init_entries(self):
        x_value  = Label(self.frame, text='x', font='Helvetica 10 bold').grid(row = self.row, column = 2)
        y_value  = Label(self.frame, text='y', font='Helvetica 10 bold').grid(row = self.row, column = 3)
        self.row += 1

    def add_entry(self):

        def validate_int_entry(text):
            if text == "":
                return True
            try:
                value = int(text)
            except ValueError:
                return False
            return value
        vcmd_int = (root.register(validate_int_entry), "%P")

        if (len(self.input_x) >= 0):
            label_counter = Label(self.frame, text=str(len(self.input_x)+1))
            label_counter.grid(row = self.row, column = 1)

        x_value = Entry(self.frame, validate = "key", validatecommand=vcmd_int, justify = RIGHT, width=10)
        x_value.grid(row = self.row, column = 2)

        y_value = Entry(self.frame, validate = "key", validatecommand=vcmd_int, justify = RIGHT, width=10)         
        y_value.grid(row = self.row, column = 3)

        self.row += 1

        self.input_x.append(x_value)
        self.input_y.append(y_value)


    def save_entry(self):

        self.x_values.clear()
        self.y_values.clear()

        for entry in self.input_x:
            x = int(entry.get())
            self.x_values.append(x)
        print(self.x_values)

        for entry in self.input_y:
            x = int(entry.get())
            self.y_values.append(x)
        print(self.y_values)


if __name__ == "__main__":
    root = Tk()

    root.resizable(0,0)
    root.title('Lot')

    lot = Window(root)
    lot.grid(row=0,column=0)

    scroll = Scrollbar(root)
    scroll.grid(row=0,column=1,sticky=N+S)

    lot.config(yscrollcommand = scroll.set)
    scroll.config(command=lot.yview)
    lot.configure(scrollregion = lot.bbox("all"), width=300, height=300)

    def add_points():
        lot.add_entry()
        lot.configure(scrollregion = lot.bbox("all"))

    b1 = Button(root, text = "Add points", command = add_points)
    b1.grid(row=1,column=0)

    def get_value():
        b1.destroy()
        lot.save_entry()

    b2 = Button(root, text = "Get value!", command = get_value)
    b2.grid(row=2,column=0)

    root.mainloop()

这是GUI的样子:

GUI

一切都是可见的,直到我添加了“第14点”。如果再次单击“添加点”,现在将显示滚动条,并且现在可以看到“第14个点”,但是新添加的点(第15个)却没有,问题将继续存在。有没有办法来解决这个问题?我看到post在谈论yview_moveto(1.0),但是我很难实现它。

0 个答案:

没有答案