Search code examples
python-3.xtkintertkinter-entry

Tkinter entry in (python) doesn't get the value


I am new to tkinter, my code's main menu entry functions work properly but the add project entry function doesn't work, I want to get addprojects entry value to a variable for further code expantion. But I am stuck in here searching every coner for a solution. No idea why only one functions entry work and others doesn't when both are same. here is my full code

from tkinter import *
from tkinter import messagebox as mb

global YourChoice
global ProjectsDetails
global AvWorkers

YourChoice = 0
ProjectsDetails = []
AvWorkers = 100

def WinSelect():
    global YourChoice
    print(YourChoice)
    if(YourChoice == 1):
        addProject()

    #Remove Completed Project - function call.
    elif(YourChoice == 2):
        rProject()

    #Add new workers.
    elif(YourChoice == 3):
        AddWorkers()

    #Update Project Details.
    if(YourChoice == 4):
        UpProject()

    #Project Statistics.
    if(YourChoice == 5):
        pStats()

    #Exit the program
    elif(YourChoice == 6):
        exit()

def addProject():
    addproject = Tk()
    addproject.geometry("700x400")
    addproject.title("Add a new project")
    
    label1 = Label(addproject, text="XYZ Company", font=("arial",20,"bold", "italic")).pack()
    label2 = Label(addproject, text="Add a new project", font=("arial",16)).pack()
    label3 = Label(addproject, text="Project Code          -     **Enter '0' to Project Code to exit.", font=("arial",10)).place(x=50,y=100)
    label4 = Label(addproject, text="Clients Name         -", font=("arial",10)).place(x=50,y=130)
    label5 = Label(addproject, text="Start date              -", font=("arial",10)).place(x=50,y=160)
    label6 = Label(addproject, text="Expected end date -", font=("arial",10)).place(x=50,y=190)
    label7 = Label(addproject, text="Number of workers -", font=("arial",10)).place(x=50,y=220)
    label8 = Label(addproject, text="Project status        -", font=("arial",10)).place(x=50,y=250)

    pc = IntVar()
    cn = StringVar()
    sd = StringVar()
    ed = StringVar()
    nw = IntVar()
    ps = StringVar()

    def submitb():
        ProjectCode = pc.get()
        print(ProjectCode)

    entry1 = Entry(addproject, textvariable = pc)
    entry1.place(x=525,y=100)
    entry2 = Entry(addproject, textvariable = cn)
    entry2.place(x=525,y=130)
    entry3 = Entry(addproject, textvariable = sd)
    entry3.place(x=525,y=160)
    entry4 = Entry(addproject, textvariable = ed)
    entry4.place(x=525,y=190)
    entry5 = Entry(addproject, textvariable = nw)
    entry5.place(x=525,y=220)
    entry6 = Entry(addproject, textvariable = ps)
    entry6.place(x=525,y=250)

    submit = Button(addproject, text="Submit Data", font=("arial",8), command=submitb).place(x=585,y=320)

def mainMenu():

    mainmenu = Tk()
    mainmenu.geometry("700x400")
    mainmenu.title("Main Menu")

    label1 = Label(mainmenu, text="XYZ Company", font=("arial",20,"bold", "italic")).pack()
    label2 = Label(mainmenu, text="Main Menu", font=("arial",16)).pack()
    label3 = Label(mainmenu, text="1. Add a new project to existing projects.", font=("arial",10)).place(x=50,y=100)
    label4 = Label(mainmenu, text="2. Remove a completed project from existing projects.", font=("arial",10)).place(x=50,y=130)
    label5 = Label(mainmenu, text="3. Add new workers to available workers group.", font=("arial",10)).place(x=50,y=160)
    label6 = Label(mainmenu, text="4. Update details on ongoing projects.", font=("arial",10)).place(x=50,y=190)
    label7 = Label(mainmenu, text="5. Project Statistics", font=("arial",10)).place(x=50,y=220)
    label8 = Label(mainmenu, text="6. Exit", font=("arial",10)).place(x=50,y=250)

    yc = IntVar()

    def submitb():
        global YourChoice
        YourChoice = yc.get()
        WinSelect()

    entry1 = Entry(mainmenu, textvariable = yc).place(x=525,y=280)

    submit = Button(mainmenu, text="Submit Data", font=("arial",8), command=submitb).place(x=585,y=320)

mainMenu()

mainloop()

Tried everything, want to asign entry functions value to a variable

Thank you in advance.


Solution

  • Your description is a bit difficult to understand, but I can give you a few pointers.

    You are using more than one instance of Tk() which may cause problems. Why are multiple instances of Tk discouraged?

    Both the function that creates the addproject window and the Tk instance it creates are called addproject.

    Hope this helps.