0

对 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()
4

2 回答 2

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()
于 2018-05-02T15:34:57.003 回答
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()

请注意,这带来了额外的优势,即当需要取出用户数据时,我们有一个可以迭代的行列表。

于 2018-05-02T15:46:29.743 回答