【问题标题】:Pass variables between modules在模块之间传递变量
【发布时间】:2020-04-19 20:11:07
【问题描述】:

我有一个 tkinter gui,它有一个基本的输入框和搜索按钮。在名为 SearchList.py 的模块中搜索列表

Main.py

from tkinter import *
from SearchList import *
root = Tk()
Output = []

def search():
    search = e.get()
    Output= SearchList.go(search)

#search bar
e= Entry(root, width=50, borderwidth=5)
e.grid(row=0, column=0, columnspan=2)

#search button
doneButton = Button(root, text="Search", width= 10, command=search).grid(row=0, column=4)


#arranges output in a 5 column rectangle
r, c = (1,0)

c_limit= 5

for thing in Output:
    myButton = Button(root, text=thing,height=5, width=25, borderwidth=3, padx=10).grid(row=r,column=c)
    c+=1
    if c == c_limit:
        c=0
        r+=1


root.mainloop()

这让我可以输入我想要搜索的内容并将其发送到 Searchlist.py 模块,并且输出(列表中的匹配项)被排列在一个矩形中。

SearchList.py

Output = []


def main(search):
    for x in range(list):
         if list[x]== search:
              Output.append(list[x])
         else:
              continue
    Output.sort
    return Output


def go(search):
    with client:
        client.loop.run_until_complete(main(search))

但它不断返回None。 我也知道我一定是错误地调用了模块——只是不确定如何。

【问题讨论】:

  • 您可能只是在return client.loop.run_until_complete(main(search)) 中缺少returnSearchList.go 中。
  • 作为一个程序员,你应该非常非常非常努力地不使用全局变量。
  • @Erich 这是我的问题,但由于某种原因,列表没有显示在 tkinter 网格中?
  • 您在search 的开头缺少global Output
  • 哦,我明白了。用重新格式化的代码回答,我会接受它

标签: python search module return parameter-passing


【解决方案1】:

这里有两个小问题。首先,您在SearchList.py 中缺少return

def go(search):
    with client:
        # insert return here
        return client.loop.run_until_complete(main(search))

其次,你没有写到search中的正确变量:

def search():
    # use global keyword to reference global variable Output
    global Output
    search = e.get()
    Output= SearchList.go(search)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-21
    • 2020-05-16
    • 2017-10-25
    • 1970-01-01
    • 2017-03-10
    • 2011-07-15
    • 1970-01-01
    • 2018-03-16
    相关资源
    最近更新 更多