【问题标题】:Set value of list box based on variable tkinter python 3.6基于变量tkinter python 3.6设置列表框的值
【发布时间】:2018-02-04 14:55:37
【问题描述】:

Python 3.6、Spyder、Tkinter

嗨,我在代码顶部的 .after 循环中有一些代码(一个函数),我在代码底部获得了两个列表框选择,我想切换这两个值。

两个列表框都填充了相同的数据。

假设我在第一个列表框中选择 #1 NZD 并在第二个列表框中选择 #2 USD 然后在我完成计算后执行一些代码计算一些东西我想将两个列表框的活动选择更改为 #1美元和#2NZD。

所以当 .after 循环回到开头并且确实如此时,

cur1 = listbox1.get('active')
cur2 = listbox2.get('active')

它得到正确的切换值。

目前我试过了。

cur1prev = cur1
cur2prev = cur2

cur1 = cur2prev
cur2 = cur1prev
listbox1.set(cur1)
listbox.set(cur2)

root.after(3000, #function for button)

有没有一种简单的方法可以做到这一点,我认为这与他们的索引值有关。

这是完整的功能,https://pastebin.com/ZrUP3VBQ

谢谢

根据要求添加了一个基本示例

import tkinter as tk


#Make Window
root = tk.Tk()
root.geometry("612x417")
root.title("Exchange Rates")
root.resizable(0,0)
root.configure(background='lightgrey')

def printsels():
    sel1 = lb1.get('active')
    sel2 = lb2.get('active')
    print (sel1, sel2)
    lb1.select_set(sel2)
    lb2.select_set(sel1)

    root.after(3000, printsels)

lb1 = tk.Listbox(root, font="bold", height=3, width=8)
lb2 = tk.Listbox(root, font="bold", height=3, width=8)

for i in range (10):
    i = i + 1
    lb1.insert("end", i)
    lb2.insert("end", i)

lb1.pack()
lb2.pack()

btn = tk.Button(root, text="Get selections", command=printsels)
btn.pack()

root.mainloop()

输出仍然是我在循环时首先选择的任何内容。

【问题讨论】:

  • 请不要链接到其他网站上的代码。 Intead,创建一个minimal reproducible example。不清楚你想要什么,所以一个显示两个列表框和足够的代码来说明你的问题的minimal程序会增加你得到有用答案的机会。
  • 按要求完成

标签: python-3.x function tkinter listbox spyder


【解决方案1】:

解决了。

在循环开始时使用。

index1 = listbox1.get(0, "end").index(cur1)
index2 = listbox2.get(0, "end").index(cur2)

在循环结束时使用。

listbox1.activate(index2)
listbox2.activate(index1)

最终代码是

def results():
    if running:
        global usraccbal
        cur1 = listbox1.get('active')
        cur2 = listbox2.get('active')

        index1 = listbox1.get(0, "end").index(cur1)
        index2 = listbox2.get(0, "end").index(cur2)

        print (index1,index2)

        frmcur_text.set(cur1)
        tocur_text.set(cur2)

        t = datetime.utcnow()  
        lbldatetime_text.set(t)


#get usr input of text box for amount to trade    this is trade 1
        usraccbal = Decimal (cnvrtamt.get())


#Get rates and assign  their values to variables rate1contents and rate2contents then print

        var1 = getrates(cur1, cur2)
        var2 = (var1[0])
        var3 = (var1[1])
        rate1contents = Decimal (var2)
        rate2contents = Decimal (var3)
        #print the exrates
        print ('currency1',cur1,' to currency2', cur2, '=',rate1contents)
        print ('currency2',cur2,' to currency1', cur1, '=',rate2contents)

#Do trade number 1 giving result as a variable cnvrt
        #Do trade number 1 as a variable 
        c = newconvert(usraccbal, rate1contents)
        #unpack values from trade1 variable for use in other calculations
        newcnvrt = Decimal (c[0])
        prevusraccbal = Decimal (c[1])
        #make variable calle cnvrt with result of newconvert function
        cnvrt = newcnvrt
        #Print the result of trade 1 and the previous accbal/amount converted
        print ('#1 Trade =',cur2, 'newaccbal', newcnvrt,'previous account balance =', prevusraccbal,)

#Update tk labels for result of converted amount and which currency we converted to
        lblconvertedamtval_text.set(newcnvrt)
        lblconvertedcur_text.set(cur2)
#Update tk labels for previous currency data        
        lblprevcur_text.set(cur1)
        lblprevcuramt_text.set(prevusraccbal)
#set user account balance again
        usraccbal = newcnvrt
        cnvrtamt.set(usraccbal)
        #Attempt to trade back to cur1
        #Store old country codes
        cur1prev = cur1
        cur2prev = cur2
#       Store previous amount that was converted
        cnvrtamtprev = Decimal (prevusraccbal)
#Store old conversion result    
        cnvrtresultprev = Decimal (newcnvrt)
#Do trade back
        cur1 = cur2prev
        cur2 = cur1prev
        listbox1.activate(index2)
        listbox2.activate(index1)
        print ('new cur1 =', cur1, 'new cur2 =',cur2)


        root.after(3000,results)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 2019-12-16
    • 2020-02-16
    • 2016-07-25
    • 2017-12-11
    • 1970-01-01
    相关资源
    最近更新 更多