如何删除Tkinter中的行?

时间:2018-05-02 15:01:18

标签: python python-2.7 tkinter tkinter-entry tkinter-layout

对python和Tkinter来说很新。

我尝试允许用户输入一个数字,然后确定显示的行数。但是,如果提交的新号码小于上一个号码,则不会删除旧行。

提前致谢!

示例代码:

import Tkinter as tk
from Tkinter import *
root = tk.Tk()

def input_count():
    try:
        user_submission=int(user_text.get())
    except:
        wrong_submission=tk.Label(root,  text="That isn't a number, try again!", justify = tk.LEFT, padx = 20)
        wrong_submission.grid(column=0 , row=1)
    else:
        for num in range(0,user_submission):
            old_row=2
            new_row=old_row+(2*num)
            extra_new_row= new_row + 1
            animal_check=tk.Label(root,  text='Enter an animal', justify = tk.LEFT, padx = 20)
            animal_check.grid(column=0 , row=new_row)
            animal_text = Entry(root, width= 50)
            animal_text.grid(column=1, row=new_row)
            colour_check=tk.Label(root,  text='Enter a colour', justify = tk.LEFT, padx = 20)
            colour_check.grid(column=0 , row=extra_new_row)
            colour_text = Entry(root, width= 50)
            colour_text.grid(column=1, row=extra_new_row)



user_label=tk.Label(root, text='Enter a number', justify = tk.LEFT, padx = 20)
user_label.grid(column=0 , row=0)
user_text= Entry(root, width= 50)
user_text.grid(column=1, row=0)
user_submit=Button(root,text="SUBMIT", command=input_count)
user_submit.grid(column=2,row=0)
root.mainloop()

2 个答案:

答案 0 :(得分:0)

您可以将根窗口拆分为两个框架,一个框架包含用户条目,另一个框架包含所有新行。然后你可以使用方法' grid_forget'在第二帧上删除它并在每次用户提交一些行时创建一个新帧。

import os, sys
from Tkinter import *


root = tk.Tk()

def input_count():
    try:
        user_submission=int(user_text.get())
    except:
        wrong_submission=tk.Label(frame_top,  text="That isn't a number, try again!", justify = tk.LEFT, padx = 20)
        wrong_submission.grid(column=0 , row=1)
    else:
        try:
            frame_bottom.grid_forget()
        except:
            pass
        frame_bottom = tk.Frame(root)
        frame_bottom.grid(row = 2, column = 0, sticky = "nsew")
        for num in range(0,user_submission):
            old_row=2
            new_row=old_row+(2*num)
            extra_new_row= new_row + 1
            animal_check=tk.Label(frame_bottom, text='Enter an animal', justify = tk.LEFT, padx = 20)
            animal_check.grid(column=0, row=new_row)
            animal_text = Entry(frame_bottom, width= 50)
            animal_text.grid(column=1, row=new_row)
            colour_check=tk.Label(frame_bottom,  text='Enter a colour', justify = tk.LEFT, padx = 20)
            colour_check.grid(column=0, row=extra_new_row)
            colour_text = Entry(frame_bottom, width= 50)
            colour_text.grid(column=1, row=extra_new_row)

frame_top = tk.Frame(root)
frame_top.grid(row = 0, column = 0, sticky = "nsew")

frame_bottom = tk.Frame(root)
frame_bottom.grid(row = 2, column = 0, sticky = "nsew")

user_label=tk.Label(frame_top, text='Enter a number', justify = tk.LEFT, padx = 20)
user_label.grid(column=0 , row=0)
user_text= Entry(frame_top, width= 50)
user_text.grid(column=1, row=0)
user_submit=Button(frame_top,text="SUBMIT", command=input_count)
user_submit.grid(column=2,row=0)

root.mainloop()

答案 1 :(得分:0)

您需要创建一个容器来保持对行的引用。为了保持整洁,让我们将一行的所有组件放入一个类中。然后我们可以使用destroy()方法销毁所有行,并使用get()方法从行中获取结果。我们将创建一个名为" current_rows"的全局列表。保留当前显示的所有行。我们可以添加到该列表以添加行,并从该列表中删除以删除行。

import Tkinter as tk
from tkMessageBox import showerror

class Mgene:
    def __init__(self, master):
        columns, rows = master.grid_size()
        self.animal_check=tk.Label(master,  text='Enter an animal', justify = tk.LEFT, padx = 20)
        self.animal_check.grid(column=0 , row=rows)
        self.animal_text = tk.Entry(master, width= 50)
        self.animal_text.grid(column=1, row=rows)
        self.colour_check=tk.Label(master,  text='Enter a colour', justify = tk.LEFT, padx = 20)
        self.colour_check.grid(column=0 , row=rows+1)
        self.colour_text = tk.Entry(master, width= 50)
        self.colour_text.grid(column=1, row=rows+1)

    def destroy(self):
        self.animal_check.destroy()
        self.animal_text.destroy()
        self.colour_check.destroy()
        self.colour_text.destroy()

    def get(self):
        return self.animal_text.get(), self.colour_text.get()

current_rows = []
def input_count():
    try:
        user_submission=int(user_text.get())
    except:
        showerror("Error", "That isn't a number, try again!")
    else:
        # add any rows needed
        for num in range(len(current_rows)-1, user_submission):
            current_rows.append(Mgene(root))

        # remove any rows needed
        while len(current_rows) > user_submission:
            current_rows.pop().destroy()

def get_answers():
    for row in current_rows:
        print row.get()

root = tk.Tk()

user_label=tk.Label(root, text='Enter a number', justify = tk.LEFT, padx = 20)
user_label.grid(column=0 , row=0)
user_text= tk.Entry(root, width= 50)
user_text.grid(column=1, row=0)
user_submit=tk.Button(root,text="SUBMIT", command=input_count)
user_submit.grid(column=2,row=0)
user_get=tk.Button(root,text="print answers", command=get_answers)
user_get.grid(column=2,row=1)
root.mainloop()

注意这提供了额外的好处,当需要获取用户数据时,我们有一个很好的行列表,我们可以迭代。