Search code examples
pythontkinterreturnreturn-value

no value being returned from program


I have the following code:

import time
import warnings
import numpy as np
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import external
import excelwork

window = Tk()
window.title('My Sample Solution')
window.geometry('700x500')
window.configure(background = 'green')
rows = 0
while rows < 50:
    window.rowconfigure(rows, weight = 1)
    window.columnconfigure(rows, weight = 1)
    rows += 1
found = ['']

#Label Input:
Label(window, text='Search For: ').grid(column = 0, row = 0)
#Dropdown select search
def DisplayDevNum():
    search_for = Srch.get()
    found = excelwork.srch_knums(int(search_for))
    #This is where you choose what to do with the information
    return found

def DisplayDevAdd():
    search_for = Srch.get()
    found = excelwork.srch_add(str(search_for))
    #This is where you choose what to do with the information
    return found

def DisplayDevCty():
    search_for = Srch.get()
    found = excelwork.srch_cty(str(search_for))
    #This is where you choose what to do with the information
    return found

def DisplayDevStt():
    search_for = Srch.get()
    found = excelwork.srch_stt(str(search_for))
    #This is where you choose what to do with the information
    return found

def DisplayStrNum():
    search_for = Srch.get()
    found = excelwork.srch_snums(int(search_for))
    #This is where you choose what to do with the information
    return found

def DisplayStrName():
    search_for = Srch.get()
    found = excelwork.srch_stnm(str(search_for))
    #This is where you choose what to do with the information
    return found


def chng_srch():
    srch_choices.get()
    if srch_choices == options[0]:
        DisplayDevNum()
        return found()
    if srch_choices == options[1]:
        DisplayDevAdd()
        return found()
    if srch_choices == options[2]:
        DisplayDevCty()
        return found()
    if srch_choices == options[3]:
        DisplayDevStt()
        return found()
    if srch_choices == options[4]:
        DisplayStrNum()
        return found()
    if srch_choices == options[5]:
        DisplayStrName()
        return found()

options = ['Device Number','Device Address','Device City','Device State','Store Number','Store Name']

srch_choices = ttk.Combobox(window, values = options)
srch_choices.grid(row = 0, column = 1)

#Input Entry
Srch = Entry(window)
Srch.grid(column = 2, row = 0)



display_tabs = ttk.Notebook(window)
display_tabs.grid(row = 3, column = 0, columnspan = 50, rowspan = 47, sticky = 'NESW')
tab1 = ttk.Frame(display_tabs)
display_tabs.add(tab1, text = 'Info')

Label(tab1, text = 'Kiosk ID: ').grid(column = 0, row = 0)
Label(tab1, text = found[0]).grid(column = 1, row = 0)


#Go Button
Button(window, text = 'Go', command = chng_srch).grid(column = 3, row = 0)



window.mainloop()

I am trying to print the value of my functions as results of a search. However, I am not getting any output from my returns. My excelwork import is a personally written file and that works. I have tested it and it returns the values expected when directly run. That said, I'm not entirely sure where I went wrong. Can somebody help?


Solution

  • There are a few separate issues with your current code.

    First, simply changing the found variable will not update the Label. You can instead make a Tkinter StringVar to hold the text you want to display, which will cause the Label to be updated whenever it changes. See the Patterns section of the Label docs for an example of this.

    The second issue, is that when you run the DisplayDevNum function, you need to save the output - running the function by itself doesn't actually do anything. So instead of doing

    if srch_choices == options[0]:
            DisplayDevNum()
            return found()
    

    you need to do something like this:

    if srch_choices == options[0]:
            return DisplayDevNum()
    

    However, simply returning a value from chng_srch will not update either found or the Label contents. To change the found variable (assuming that found is now a StringVar used by the Label), you don't need to return anything from your functions, but you do need to mark found as a global variable (note that many people consider global variables bad practice in most situations). Another option would be to keep the Label as a variable, and have the chng_srch function explicitly update the Label contents.