【问题标题】:Place in a file from drop down list从下拉列表中放入文件
【发布时间】:2016-03-27 13:19:38
【问题描述】:

如何找出从下拉框中选择的选项在文本文件中的位置? 我有一个存储在新行上的不同值的文本文件。然后我将它加载到一个数组中,该数组形成下拉框的值。我想知道用户选择的值的行位置。到目前为止,我的代码包含在下面。

from tkinter import *


class search():
def __init__ (self, master):

  self.master = master
  self.master.title("Search For Quotes Screen")
  self.master.geometry("2100x1400")
  self.master.configure(background = 'white')

  self.master.configure(background = "white")
  self.Title = Label(self.master, text = "Quote Retrieval", font = ("calibri", 20), bg = "White")
  self.Title.place(x=650, y = 10)


  array=[]#initialises array
  with open('PostCode_File.txt', 'r') as f:# opens name of your file
      array= [line.strip() for line in f]#puts values of file in array. each line = one part of array
  f.close()



  Option = StringVar()
  Option.set("Please select postcode of quote")
  self.options = OptionMenu(self.master, Option, *array)#creates drop down menu
  self.options.config(bg = 'navy', fg='white', font =('calibri', 20))
  self.options["menu"].config(bg="Navy", font=('calibri', 13), fg= 'white')
  self.options.place(x=100, y=100)

  print( self.options.get())

我注意到一个下拉框没有像文本输入框那样的功能,或者我可能用错了。任何帮助,将不胜感激。

【问题讨论】:

  • 你的价值观是独一无二的吗?
  • 大部分值是一样的,但也可能有一些值是一样的

标签: python user-interface python-3.x tkinter tk


【解决方案1】:

如果您的输入文件中有独特的行,您可以通过保留对选项数组的引用然后在该数组上调用 index 来获取所选项目的位置来完成此操作。注意get() 函数不在OptionsMenu 对象上;它位于StringVar 对象上,这意味着您还需要保留对它的引用。例如:

from tkinter import *


class Search():
    def __init__(self, master):

        self.master = master
        self.master.title("Search For Quotes Screen")
        self.master.geometry("2100x1400")
        self.master.configure(background='white')

        self.master.configure(background="white")
        self.Title = Label(self.master, text="Quote Retrieval", font=("calibri", 20), bg="White")
        self.Title.place(x=650, y=10)

        with open('foo.txt', 'r') as f:  # opens name of your file
            self.array = [line.strip() for line in f]  # puts values of file in array. each line = one part of array
        f.close()

        self.option = StringVar()
        self.option.set("Please select postcode of quote")
        self.options = OptionMenu(self.master, self.option, *self.array)  # creates drop down menu
        self.options.config(bg = 'navy', fg='white', font =('calibri', 20))
        self.options["menu"].config(bg="Navy", font=('calibri', 13), fg='white')
        self.options.place(x=100, y=100)

        self.button = Button(master, text='OK', command=self.on_ok)
        self.button.place(x=100, y=150)

    def on_ok(self):
        value = self.option.get()
        print(value, self.array.index(value))


if __name__ == '__main__':
    master = Tk()
    search = Search(master)
    mainloop()

但是,如果输入文件中的行不是唯一的,这将不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 2016-10-15
    • 2021-05-26
    相关资源
    最近更新 更多