【发布时间】:2019-10-25 12:42:19
【问题描述】:
我在 Python 中使用 Tkinter,并且我有一个包含一组元素的选项菜单:
客户
工作坊
供应链
然后我有一个名为 "RestRout3.txt" 的文本文件,其中包含一个长列表:
Supplychain/dealer/etc.
Supplychain/parts/etc.
Supplychain/shop/etc.
Supplychain/order/etc.
Supplychain/tasks/etc.
Supplychain/payment/etc.
Workshop/location/etc.
Workshop/name/etc.
Workshop/report/etc.
Customer/info/etc.
Customer/order/etc.
在我的应用程序中,我有一个按钮,点击它会运行这个方法def Action():
通过做一些 if 语句,我想让两个相同的词都匹配。
def Action():
appName = variabel.get() #Element selected from Optionmenu
with open("RestRout3.txt", "r") as f:
content = f.readlines()
print (content) # reads all the content in file
return content
if appName == content[0]: # for example appname(supplychain) and content(supplychain)
print ("Both words are matching")
我希望这两个词是匹配的,所以我以后可以做更多的功能。
感谢我能得到的所有帮助,谢谢。 我是 Python 新手。
【问题讨论】:
-
“我希望这两个词”这两个词非常通用。如果您实际上解释了您要查找的单词以及您要查找的单词,这将有所帮助。目前,您的问题对我来说是模糊的回答。解决此问题的最佳方法是首先确保解释所有内容,然后提供示例输入和输出。
-
例如我试图在 RestRout3.txt 中找到“供应链”。然后我想将该词与 Optionmenu 中的一项进行比较。如果它们匹配,我会打印出:“Both words are matching”。
-
“供应链”是一个词而不是两个词。您明确地说“我希望这两个词都匹配”。请仔细检查您的问题,并确保您正确解释了所有内容。
-
代码
if appName == content[0]:将永远不会被执行,因为它前面有return content语句。此外,如果appName是"supplychain",但content[0]是"Supplychain/dealer/etc",那么if 条件将始终为False。
标签: python loops if-statement tkinter text-files