mardi 30 octobre 2018

Trying a date within Tkinter - Python

After testing out many times and reading Stackoverflow for several hours, I decided to right this question. My Text (part of the bigger code) is below:

import pandas as pd
import datetime as dt
from tkinter import *
import tkinter.filedialog
from tkinter import messagebox

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

def load_click():
  global item_prosp
  item_prosp = pd.read_excel(a1)

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

 def save_click():
   window.destroy()

 ##Tk_Main:
 window = Tk()
 window.title("File selection window")
 window.configure(background = "white")
 window.geometry("480x700")

 #File1
 label2 = Label(window, text="Select the item prospective file", bg="white").grid(row=2, column=0,columnspan=3, pady=2, sticky=W)
 output1 = Entry(window, width=60, background="gray")
 output1.grid(row=3, column=0, columnspan=3, padx=10, sticky=W)
 Button(window, text="Search", width=6, command=click1).grid(row=3, column=3, padx=5, sticky=W)

 #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)

 #Exit   
 Button(window, text="Generate", width=13, command=save_click).grid(row=20, column=0, columnspan=3, padx=10, pady=10, sticky=W)
 window.mainloop()

My need is to import a file (externally built and already structured), read 2 values from it (variables Z and W in the code) and compare it with an input variable (ipt_dt) which is a date filled in by the user through 3 dropdown menus from tkinter.

The error is that the try is not going through the if statement and it never prints out if the input is outside the scope date. Everytime I enter a date smaller than the minimum date or higher than the maximum date it returns the showerror message eventhou it pritns the "Date ok".

Anyone has any idea on how to solve this or why my fi is being ignored?

Thanks!

Aucun commentaire:

Enregistrer un commentaire