如何在不结束程序的情况下绘制用户输入数据?

2024-04-25 20:54:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前有一个使用tkinter的GUI设置,它请求用户数据并将数据添加到列表中。每次按下“提交”按钮时,数据都会附加到列表中。我需要做的是关闭tkinter窗口,在不结束程序和丢失数据的情况下移动到散点图。你知道吗

我已经取消了在下面的代码示例中添加散点图的尝试。我需要在x轴上画出“周期”,在Y轴上画出“应力”。你知道吗

感谢您的帮助。你知道吗

import tkinter as tk
from tkinter.simpledialog import askstring, askinteger
from tkinter.messagebox import showerror


test_log = []
stress = []
cycles = []

def display_1():
    test_log_input = entry_1.get()
    # Try convert a str to int
    # If unable eg. int('hello') or int('5.5')
    # then show an error.
    try:
       test_log_input = int(test_log_input)
    # ValueError is the type of error expected from this conversion
    except ValueError:
        #Display Error Window (Title, Prompt)
        showerror('Non-Int Error', 'Please enter an integer')
    else:
        list.append(test_log, test_log_input)
        print("Test Log #: " + str(test_log))

    stress_input = entry_2.get()
    # Try convert a str to int
    # If unable eg. int('hello') or int('5.5')
    # then show an error.
    try:
       stress_input = int(stress_input)
    # ValueError is the type of error expected from this conversion
    except ValueError:
        #Display Error Window (Title, Prompt)
        showerror('Non-Int Error', 'Please enter an integer')
    else:
        list.append(stress, stress_input)
        print("Stress (MPa): " + str(stress))

    cycles_input = entry_3.get()
    # Try convert a str to int
    # If unable eg. int('hello') or int('5.5')
    # then show an error.
    try:
       cycles_input = int(cycles_input)
    # ValueError is the type of error expected from this conversion
    except ValueError:
        #Display Error Window (Title, Prompt)
        showerror('Non-Int Error', 'Please enter an integer')
    else:
        list.append(cycles, cycles_input)
        print("Cycles until failure: " + str(cycles))

    #data1 = {'Cycles': [cycles], 'Stress (MPa)': [stress]}
    #df1 = DataFrame(data1, columns=['Cycles', 'Stress (MPa)'])


# Create the main window
root = tk.Tk()

# Create the title

root.title("HCF Ti64 Equiaxed Microstructure SN Diagram")

# Create the widgets
entry_1 = tk.Entry(root)
entry_2 = tk.Entry(root)
entry_3 = tk.Entry(root)
btn_1 = tk.Button(root, text = "Submit", command = display_1)

# Grid is used to add the widgets to root
# Alternatives are Pack and Place
tk.Label(root, text = "Enter the test log number: ").grid(row=0)
entry_1.grid(row = 0, column = 1)
tk.Label(root, text = "Enter the stress (MPa): ").grid(row=1)
entry_2.grid(row = 1, column = 1)
tk.Label(root, text = "Enter the cycles until failure:  ").grid(row=2)
entry_3.grid(row = 2, column = 1)
btn_1.grid(row = 3, column = 0)


root.mainloop()

Tags: thetestanloginputerrorroottk