繁体   English   中英

如何传递或延迟从 tkinter 中的输入字段获取零值?

[英]How to pass or delay getting zero values from the entry fields in tkinter?

我正在 tkinter 中编写代码以生成网格化的 plot。要运行代码,您可以在nxny输入框中输入5 ,然后打开一个包含25数字的文本文件,例如: 1 3 5 6 1 0 3 5 6 4 2 4 7 3 0 9 8 3 1 3 5 7 8 8 1 ,然后按Plot ,生成网格 plot 。 并通过移动滑块并再次按下Plot ,轴范围发生变化。

但是,有一个问题,我为滑块输入了max_val=5 我想从输入字段中获取此值,例如: max_val=data.nx.get() 但是当程序运行时,我的输入字段最初zero ,并且会引发错误。 在输入框中输入值后,有没有办法通过 slider 小部件的执行?

import tkinter as tk
from tkinter import *
from tkinter import filedialog
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import cm
from matplotlib.colors import ListedColormap
from RangeSlider.RangeSlider import RangeSliderH, RangeSliderV

global nums
class Data:
    def __init__(self):
        self.nx = tk.IntVar()
        self.ny = tk.IntVar()

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        container = tk.Frame(self)
        container.pack()

        self.data = Data()

        self.frames = {}
        for F in (PageOne, PageTwo):
            frame = F(container, self.data)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.frames[PageOne].button3_P1.config(command=self.go_to_page_two)
        self.show_frame(PageOne)

    def go_to_page_two(self):
        self.show_frame(PageTwo)

    def show_frame(self, c):
        frame = self.frames[c]
        frame.tkraise()

class PageOne(tk.Frame):
    def __init__(self, parent, data):
        super().__init__(parent)
        self.data = data

        frame_1 = tk.LabelFrame(self, text="Inputs", labelanchor='n', height=400, width=280)
        frame_1.grid(row=0, column=0, padx=5, sticky='n')

        label_1 = tk.Label(frame_1, text="nx:")
        label_1.grid(row=0, column=0)

        self.entry_1 = tk.Entry(frame_1, textvariable=data.nx, width=7)
        self.entry_1.grid(row=0, column=1)

        label_2 = tk.Label(frame_1, text="ny:")
        label_2.grid(row=1, column=0)

        self.entry_2 = tk.Entry(frame_1, textvariable=data.ny, width=7)
        self.entry_2.grid(row=1, column=1, padx= 10, pady=10)
        global nums

        def opentext():
            global nums
            my_file = filedialog.askopenfilenames(initialdir="/pycharm", title="Select your file")
            for T in my_file:
                import re
                with open(T, 'r') as infile1:
                    lines = infile1.readlines()
                    B = [x for x in lines if not x.startswith('--')]
                    C = " ".join(B)
                    nums = []
                    for n in re.findall(r'\d+', C):
                        split_by_ast = n.split("*")
                        if len(split_by_ast) == 1:
                            nums += [float(split_by_ast[0])]
                        else:
                            nums += [float(split_by_ast[1])] * int(split_by_ast[0])

        self.button1 = tk.Button(frame_1, text="Open file", command=opentext, width=10)
        self.button1.grid(row=2, column=0, pady=10, columnspan=2)

        frame_2 = tk.LabelFrame(self, text="Convas", labelanchor='n', height=550, width=800)
        frame_2.grid(row=0, column=1, padx=5)
        frame_2.grid_propagate(0)

        def plot_init():
            global H, V
            x, y = np.mgrid[slice(0, 6, 1), slice(0, 6, 1)]
            z = np.reshape(nums, (5, 5), order='F')

            figure = Figure(figsize=(7.3, 4.8))
            ax = figure.add_subplot(111)

            col_type = cm.get_cmap('rainbow', 256)
            newcolors = col_type(np.linspace(0, 1, 1000))
            white = np.array([1, 1, 1, 1])
            newcolors[:1, :] = white
            newcmp = ListedColormap(newcolors)

            c = ax.pcolormesh(x, y, z, cmap=newcmp, edgecolor='lightgrey', linewidth=0.003)
            ax.figure.colorbar(c)

            ax.set_title('Title', fontweight="bold")
            ax.set_xlabel("X", fontsize=14)
            ax.set_ylabel("Y", fontsize=14)

            canvas = FigureCanvasTkAgg(figure, frame_2)
            canvas.get_tk_widget().place(x=80, y=0)
            figure.patch.set_facecolor('#f0f0f0')

            ax.set_xlim(rs1.getValues())
            ax.set_ylim(rs2.getValues())

            rs1.place(x=150, y=470)
            rs2.place(x=0, y=50)

        hVar1 = IntVar()  # left handle variable
        hVar2 = IntVar()  # right handle variable
        rs1 = RangeSliderH(frame_2, [hVar1, hVar2], Width=500, Height=55, padX=17, min_val=0, max_val=5, font_size=12,
                           show_value=True, digit_precision='.0f', bgColor='#f0f0f0', line_s_color='black',
                           line_color='black', bar_color_inner='black', bar_color_outer='#f0f0f0')

        vVar1 = IntVar()  # top handle variable
        vVar2 = IntVar()  # down handle variable
        rs2 = RangeSliderV(frame_2, [vVar1, vVar2], Width=81, Height=390, padY=11, min_val=0, max_val=5, font_size=12,
                           show_value=True, digit_precision='.0f', bgColor='#f0f0f0', line_s_color='black',
                           line_color='black', bar_color_inner='black', bar_color_outer='#f0f0f0')

        self.button2 = tk.Button(frame_1, text="Plot", command=plot_init, width=10)
        self.button2.grid(row=3, column=0, columnspan=2)

        self.button3_P1 = tk.Button(self, text="Continue")
        self.button3_P1.grid(row=2, column=0, columnspan=4, pady=5)


class PageTwo(tk.Frame):
    def __init__(self, parent, data):
        super().__init__(parent)
        self.data = data

app = SampleApp()
app.mainloop()

目前,我收到此错误。

Traceback (most recent call last):
  File "C:\Users\pymnb\Desktop\pycharm\Test.py", line 145, in <module>
    app = SampleApp()
  File "C:\Users\pymnb\Desktop\pycharm\Test.py", line 30, in __init__
    frame = F(container, self.data)
  File "C:\Users\pymnb\Desktop\pycharm\Test.py", line 122, in __init__
    rs1 = RangeSliderH(frame_2, [hVar1, hVar2], Width=500, Height=55, padX=17, min_val=0, max_val=data.nx.get(), font_size=12,
  File "C:\Users\pymnb\AppData\Local\Programs\Python\Python39\lib\site-packages\RangeSlider\RangeSlider.py", line 107, in __init__
    pos = (value-min_val)/(max_val-min_val)
ZeroDivisionError: division by zero

假设input filenxny是强制输入。 在将它们传递给 plot function 之前,您应该检查其正确值。

如果nxny等于 0 是非法的,您必须检查它是否大于 0 然后检查用户是否选择了input file

您应该对您的代码进行三处更改:

  • plot_init方法和 plot 中移动你的滑块代码一次
  • 将变量nums初始化为None ,以检查是否选择了input file
  • 检查所有强制值并在不满足条件时显示错误消息

此外,您可以将数据初始化为不同于 0 的默认值:

class Data:
    def __init__(self):
        self.nx = tk.IntVar(value=1)
        self.ny = tk.IntVar(value=1)

这是修改后的plot_init方法,前面有nums = None

nums = None
self.rs1, self.rs2 = None, None
def plot_init():
    global H, V, nums
    x_max = data.nx.get()
    y_max = data.ny.get()

    if x_max == 0 or y_max == 0:
        tk.messagebox.showerror(title="Entry Error", message="Check Your input, nx > 0 and ny > 0!")
        return
    if nums is None:
        tk.messagebox.showerror(title="Entry Error", message="Check Your input, no file was opened!")
        return

    x, y = np.mgrid[slice(0, 6, 1), slice(0, 6, 1)]
    z = np.reshape(nums, (5, 5), order='F')

    figure = Figure(figsize=(7.3, 4.8))
    ax = figure.add_subplot(111)

    col_type = cm.get_cmap('rainbow', 256)
    newcolors = col_type(np.linspace(0, 1, 1000))
    white = np.array([1, 1, 1, 1])
    newcolors[:1, :] = white
    newcmp = ListedColormap(newcolors)

    c = ax.pcolormesh(x, y, z, cmap=newcmp, edgecolor='lightgrey', linewidth=0.003)
    ax.figure.colorbar(c)

    ax.set_title('Title', fontweight="bold")
    ax.set_xlabel("X", fontsize=14)
    ax.set_ylabel("Y", fontsize=14)

    canvas = FigureCanvasTkAgg(figure, frame_2)
    canvas.get_tk_widget().place(x=80, y=0)
    figure.patch.set_facecolor('#f0f0f0')

    if self.rs1 is None:
        self.rs1 = RangeSliderH(frame_2, [IntVar(), IntVar()], Width=500, Height=55, padX=17, min_val=0, max_val=data.nx.get(),
                           font_size=12,
                           show_value=True, digit_precision='.0f', bgColor='#f0f0f0', line_s_color='black',
                           line_color='black', bar_color_inner='black', bar_color_outer='#f0f0f0')
        self.rs1.place(x=150, y=470)

    if self.rs2 is None:
        self.rs2 = RangeSliderV(frame_2, [IntVar(), IntVar()], Width=81, Height=390, padY=11, min_val=0, max_val=data.ny.get(),
                           font_size=12,
                           show_value=True, digit_precision='.0f', bgColor='#f0f0f0', line_s_color='black',
                           line_color='black', bar_color_inner='black', bar_color_outer='#f0f0f0')
        self.rs2.place(x=0, y=50)

    ax.set_xlim(self.rs1.getValues())
    ax.set_ylim(self.rs2.getValues())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM