程序没有返回任何值
no value being returned from program
我有以下代码:
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()
我正在尝试打印我的函数值作为搜索结果。但是,我的 returns 没有得到任何输出。我的 excelwork 导入是一个个人编写的文件,并且可以正常工作。我已经对其进行了测试,它 returns 直接 运行 时的预期值。也就是说,我不完全确定我哪里出错了。有人可以帮忙吗?
您当前的代码存在一些不同的问题。
首先,简单地更改 found
变量不会更新 Label
。您可以改用 Tkinter StringVar
来保存要显示的文本,这将导致 Label
在更改时进行更新。有关此示例,请参阅 Label
docs 的模式部分。
第二个问题是,当您 运行 使用 DisplayDevNum
函数时,您需要保存输出 - 运行 函数本身实际上并没有做任何事情。所以与其做
if srch_choices == options[0]:
DisplayDevNum()
return found()
你需要做这样的事情:
if srch_choices == options[0]:
return DisplayDevNum()
但是,简单地 returning 来自 chng_srch
的值将不会更新 found
或标签内容。要更改 found
变量(假设 found
现在是 Label
使用的 StringVar
),您不需要 return 函数中的任何内容, 但你确实需要 mark found
as a global variable (请注意,在大多数情况下,许多人认为全局变量是不好的做法)。另一种选择是将 Label
保留为变量,并让 chng_srch
函数显式更新 Label
内容。
我有以下代码:
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()
我正在尝试打印我的函数值作为搜索结果。但是,我的 returns 没有得到任何输出。我的 excelwork 导入是一个个人编写的文件,并且可以正常工作。我已经对其进行了测试,它 returns 直接 运行 时的预期值。也就是说,我不完全确定我哪里出错了。有人可以帮忙吗?
您当前的代码存在一些不同的问题。
首先,简单地更改 found
变量不会更新 Label
。您可以改用 Tkinter StringVar
来保存要显示的文本,这将导致 Label
在更改时进行更新。有关此示例,请参阅 Label
docs 的模式部分。
第二个问题是,当您 运行 使用 DisplayDevNum
函数时,您需要保存输出 - 运行 函数本身实际上并没有做任何事情。所以与其做
if srch_choices == options[0]:
DisplayDevNum()
return found()
你需要做这样的事情:
if srch_choices == options[0]:
return DisplayDevNum()
但是,简单地 returning 来自 chng_srch
的值将不会更新 found
或标签内容。要更改 found
变量(假设 found
现在是 Label
使用的 StringVar
),您不需要 return 函数中的任何内容, 但你确实需要 mark found
as a global variable (请注意,在大多数情况下,许多人认为全局变量是不好的做法)。另一种选择是将 Label
保留为变量,并让 chng_srch
函数显式更新 Label
内容。