# -*- encoding: utf-8 -*- """ @Author: packy945 @FileName: input.py @DateTime: 2023/7/19 16:59 @SoftWare: PyCharm """ import tkinter as tk import numpy as np import data as gl_data from tkinter import * global top def input_num(root_tk): global top top = tk.Toplevel(root_tk) label1 = Label(top, text="坐标点个数") label1.grid(row=0) # 这里的side可以赋值为LEFT RTGHT TOP BOTTOM num1 = IntVar() entry1 = Entry(top, textvariable=num1) num1.set(0) entry1.grid(row=0, column=1) Label(top, text=" ").grid(row=0, column=3) Button(top, text="确定", command=lambda: input_data(root_tk, int(entry1.get()))).grid(row=0, column=3) top.mainloop() def input_data(root_tk, num): global top sample_x = [] sample_y = [] sample_data = [] top.destroy() top = tk.Toplevel(root_tk) def add_sample_data(): try: x = float(entry_x.get()) y = float(entry_y.get()) except: label_status.config(text="输入不合法") return entry_x.delete(0, tk.END) entry_y.delete(0, tk.END) if min(x, y) < gl_data.low or max(x, y) > gl_data.high: label_status.config(text="输入超过范围") return elif len(sample_data) < num: label_status.config(text="点对已添加") sample_data.append((x, y)) sample_x.append(x) sample_y.append(y) else: label_status.config(text="已达到最大数量") def check_sample_data(): if len(sample_data) == num: label_status.config(text="已达到最大数量") gl_data.x = np.array(sample_x) gl_data.y = np.array(sample_y) print('已添加', sample_data) top.destroy() else: label_status.config(text="还需输入{}个点对".format(num - len(sample_data))) print(sample_data) label_x = tk.Label(top, text="X 值:") label_x.pack() entry_x = tk.Entry(top) entry_x.pack() label_y = tk.Label(top, text="Y 值:") label_y.pack() entry_y = tk.Entry(top) entry_y.pack() button_add = tk.Button(top, text="添加", command=add_sample_data) button_add.pack() button_check = tk.Button(top, text="检查", command=check_sample_data) button_check.pack() label_status = tk.Label(top, text="") label_status.pack() top.mainloop() if __name__ == '__main__': root = tk.Tk() input_num(root)