0

I am gettinetting the below error while running the below code, its seems like because Int function is provide with blank without any string. But how to reslove it

Error: year = int(year_field.get())

ValueError: invalid literal for int() with base 10: ''

import calendar module
import calendar

from tkinter import *

#This function displays calendar for a given year
def showCalender():
    gui = Tk()
    gui.config(background='grey')
    gui.title("Calender for the year")
    gui.geometry("550x600")
    year = int(year_field.get())
    gui_content= calendar.calendar(year)
    calYear = Label(gui, text= gui_content, font= "Consolas 10 bold")
    calYear.grid(row=5, column=1,padx=20)
    gui.mainloop()
    
#Driver code
if __name__=='__main__':
    new= Tk()
    new.config(background="grey")
    new.title("Calendar")
    new.geometry("250x140")
    cal= Label(new, text="Calendar", bg='grey', font=("times", 28, "bold"))
    
    #Label for enter year
    year= Label(new, text="Enter year", bg= 'dark grey')
    
    #text box for year input
    year_field =Entry(new)
    button = Button(new, text='Show Calender', fg='Black', bg='Blue', command=showCalender())    
    #adjusting widgets in position
    cal.grid(row=1, column=1)
    year.grid(row=2, column=1)
    year_field(row=2, column=1)
    year_field.grid(row=3, column=1)
    button.grid(row=4, column=1)
    Exit.grid(row=6, column=1)
    new.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Bitu
  • 11
  • Welcome to Stack Overflow. Which programming language is that? In order that those who know about the language notice your question, be sure to mention the language and add the appropriate language tag. – Ole V.V. Nov 13 '21 at 14:22
  • Apparently similar question — for Python: [ValueError: invalid literal for int() with base 10: ''](https://stackoverflow.com/questions/1841565/valueerror-invalid-literal-for-int-with-base-10) – Ole V.V. Nov 13 '21 at 14:23
  • This is telling you that `year_field.get()` is returning an empty string. You can't convert an empty string to an int. – Bryan Oakley Nov 13 '21 at 17:04
  • The issue is on `command=showCalender()`, it executes the function immediately without any input from user. Change it to `command=showCalender`. – acw1668 Nov 14 '21 at 00:45

1 Answers1

0

You should check if the user has input something into year_field:

def showCalender():
    gui = Tk()
    gui.config(background='grey')
    gui.title("Calender for the year")
    gui.geometry("550x600")
    year_str = year_field.get())
    if year_str: # year_str is not empty, can convert
        year = int(year_str)
    else:
        year = 0 # you can put any number here that you think fit.
    gui_content= calendar.calendar(year)
    calYear = Label(gui, text=gui_content, font="Consolas 10 bold")
    calYear.grid(row=5, column=1, padx=20)
    gui.mainloop()

The whole if / else part can be reduced to one line:

def showCalender():
    gui = Tk()
    gui.config(background='grey')
    gui.title("Calender for the year")
    gui.geometry("550x600")
    year = int(year_field.get() or 0)
    gui_content = calendar.calendar(year)
    calYear = Label(gui, text=gui_content, font="Consolas 10 bold")
    calYear.grid(row=5, column=1, padx=20)
    gui.mainloop()
TheEagle
  • 5,808
  • 3
  • 11
  • 39