经过多次测试并阅读了数小时的Stackoverflow之后,我决定纠正这个问题。我的文字(较大代码的一部分)如下:
import pandas as pd
import datetime as dt
from tkinter import *
import tkinter.filedialog
from tkinter import messagebox
def test_click():
global ipt_dt
global coef
global z
global w
z = item_prosp['Accrual_Start'].min()
w = item_prosp['Accrual_End'].max()
ipt_d = tkvar_d.get()
ipt_m = tkvar_m.get()
ipt_y = tkvar_y.get()
x = 0
while x == 0:
ipt = str(ipt_d + '/'+ ipt_m + '/' + ipt_y)
try:
ipt_dt = dt.datetime.strptime(ipt, "%d/%b/%Y")
if ipt_dt < z or ipt_dt > w:
messagebox.showinfo("Error", "The input date is outside scope date")
else:
print("Date ok")
x =+ 1
except:
messagebox.showerror("Error", "The input date is not valid")
ipt_d = 0
ipt_m = 0
ipt_y = 0
continue
生成输入的代码的tkinter部分为:
#Question 1 - Evaluation date
label4 = Label(window, text='Please inform the valuation date :', bg='white').grid(row=13, column=0, columnspan=3, pady=2, sticky=W)
tkvar_d = StringVar(window)
tkvar_m = StringVar(window)
tkvar_y = StringVar(window)
choices_d = ['1', '2', '3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']
choices_m = ['Jan', 'Feb', 'Mar','Apr','May','Jun','Jul','Aug','Sep','Oct', 'Nov', 'Dec']
choices_y = ['2018','2019', '2020', '2021','2022','2023','2024','2025','2026','2027','2028','2029','2030']
popupmenu_d = OptionMenu(window, tkvar_d, *choices_d)
popupmenu_m = OptionMenu(window, tkvar_m, *choices_m)
popupmenu_y = OptionMenu(window, tkvar_y, *choices_y)
label5 = Label(window, text='Day :', bg='white').grid(row=14, column=0, sticky=E+W)
popupmenu_d.grid(row=15, column=0, padx=2, sticky=E+W)
label6 = Label(window, text='Month :', bg='white').grid(row=14, column=1, sticky=E+W)
popupmenu_m.grid(row=15, column=1, padx=2, sticky=E+W)
label7 = Label(window, text='Year :', bg='white').grid(row=14, column=2, sticky=E+W)
popupmenu_y.grid(row=15, column=2, padx=2, sticky=E+W)
Button(window, text="Test Date", width=10, command=test_click).grid(row=15, column=3, padx=5, pady=10, sticky=W)
运行文件时W的样本值是:
2018-04-18 00:00:00
对于Z是:
2018-04-18 00:00:00
我需要导入一个文件(外部构建并已结构化),从中读取2个值(代码中的变量Z和W),并将其与输入变量(ipt_dt)进行比较,该变量是用户通过3填写的日期tkinter的下拉菜单。
错误是try不会通过if语句,并且如果输入不在合并范围日期之内,它将永远不会输出。每当我输入的日期小于最小日期或大于最大日期时,如果将日期设置为“Date ok”,它将返回showerror消息。
任何人都对如何解决这个问题有任何想法,或者为什么我的fi被忽略了?
谢谢!
最佳答案
我查看了您最初发布的代码,然后使用load_click函数将Excel加载到df中。但是实际上您不会在任何地方运行load_click函数,因此不会加载数据帧,因此不会填充z
和w
。
如果您按如下方式更改click1()函数,则它应该可以工作(它对我来说有一些示例数据)。
def click1():
global a
a = tkinter.filedialog.askopenfilename(initialdir = "/",title = "Select file", filetypes = ( ("Excel file", "*.xlsx"), ("All files", "*.*") ) )
output1.insert(END, a)
global a1
a1 = output1.get()
load_click()
或根据需要添加一个单独的“加载”按钮(在#File1部分的底部):
Button(window, text="Load", width=6, command=load_click).grid(row=4, column=3, padx=5, sticky=W)
您可能还想在if语句中添加另一个
x = 1
。否则,由于while循环,消息框将继续 pop ,从而无法更正输入日期。x = 0
while x == 0:
ipt = str(ipt_d + '/'+ ipt_m + '/' + ipt_y)
try:
ipt_dt = dt.datetime.strptime(ipt, "%d/%b/%Y")
print type(ipt_dt)
if (ipt_dt < z) or (ipt_dt > w):
messagebox.showinfo("Error", "The input date is outside scope date")
x = 1 # I've added this one
else:
print("Date ok")
x =+ 1
except:
messagebox.showerror("Error", "The input date is not valid")
ipt_d = 0
ipt_m = 0
ipt_y = 0
continue
关于python - 在Tkinter中尝试约会-Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53067479/