是否可以使用tkinter抓取标签变量并将其放入列表

时间:2018-10-04 23:15:48

标签: python user-interface tkinter

我创建了一个温度转换器程序,在其中将条目小部件的计算出的温度显示在单独的标签中,我需要做的就是获取转换后的变量并将其放入列表中。

我认为将连接的入口小部件制作为标签小部件将在它们连接的地方工作,因此我可以使用.get方法获取变量,但看起来非常混乱。我还有其他方法可以继续吗?

这是我的第一篇文章,我是Python的初学者,如果代码看起来凌乱并且包含的​​代码太多,我感到非常抱歉。

data = []
tempVal = "Celcius"

def store_temp(sel_temp):
    global tempVal
    tempVal = sel_temp

class Calculator:
    def __init__(self, num_a, num_b):
        self.num_a= num_a
        self.num_b = num_b

def convert(self):
    if tempVal == 'Fahrenheit':
        return float((float(self.num_a) - 32)* 5 / 9)
    if tempVal == 'Celcius':
        return float((float(self.num_a) * 9/ 5) + 32)

def display_add(entry_numa,entry_numb,label_answer):
   #get the value from entry_numa
    num_a = entry_numa.get()
    num_b = entry_numb.get()

num_a = str(num_a)
num_b = str(num_b)
#create an object
global data
calc = Calculator(num_a,num_b)

label_answer['text'] = calc.convert()

data += [calc]

def calc_history():
global data 
#creat e another window
window_calc_list = Tk()
window_calc_list.geometry("400x200")

#create a listbox

listbox_calc_list = Listbox(window_calc_list, width= 300)
listbox_calc_list.pack()
listbox_calc_list.insert(END, "list of data")

for info in data:

   listbox_calc_list.insert(END, str(info.num_a) + " " + str(info.num_b) + " " )

window_calc_list.mainloop()

def main():
window = Tk()
window.geometry("500x150")

validate_letter = window.register(only_letters)
validate_nb = window.register(only_numbers_max_3)


label = Label(window, width = 30, background = 'lightblue', text='enter temperature, only numbers')
label.grid(row=0, column=0)

entry_numa = Entry(window, width = 30, validate="key", validatecommand=(validate_nb, '%d', '%P'))
entry_numa.grid(row = 0, column = 1)

#create another label and entry object for num_b

label_numb = Label(window, width = 30, background = 'lightblue', text='enter location, only letters')
label_numb.grid(row=1, column=0)

entry_numb = Entry(window, width = 30, validate="key", validatecommand=(validate_letter, '%d', '%S'))
entry_numb.grid(row = 1, column = 1)

#create another label to display answer
label_answer = Label(window, width = 30, background = 'lightyellow')
label_answer.grid(row = 2, column = 1)

entry_answer = Entry(window, width = 30)
entry_answer.grid(row = 2, column = 0)


button_add = Button(window, text = "ADD", command = lambda: display_add(entry_numa,entry_numb,label_answer))
button_add.grid(row=3, column = 0)


button_delete = Button(window, text = "DELETE", command = lambda: delete_data(data))
button_delete.grid(row=3, column = 2)

#create another button to display all previous calculations
button_display = Button(window,text = "calc_history", command = lambda: calc_history())
button_display.grid(row=3, column = 1)

var = StringVar()

dropDownList = ["Celcius", "Fahrenheit"]
dropdown = OptionMenu(window, var,dropDownList[0], *dropDownList, command=store_temp)
dropdown.grid(row=0, column=2)


window.mainloop()

2 个答案:

答案 0 :(得分:0)

tk.Label显示的值可以通过text属性访问 ,labelwidgetname ['text']。 取决于何时以及如何需要独立的存储值列表 要更新,有多种选择。该示例显示了一个 用户需要按下提交按钮。这可以改编, 例如,当执行温度计算时。

当然,在脚本中已导出计算得出的标签文本温度的位置直接更新存储值列表会更简单。

import tkinter as tk
stored_values = []

def add_labelvalue_tolist(temp):
    '''Store label value to list.'''
    stored_values.append(temp)
    print('contents of list', stored_values)

def add_entry_tolabel(event):
    display_label['text'] = user_entry.get()

ROOT = tk.Tk()

user_entry = tk.Entry()
user_entry.grid(column=0, row=0)
user_entry.bind('<KeyRelease>', add_entry_tolabel)
display_label = tk.Label()
display_label.grid(column=1, row=0)

# update list via button command linked to label text value
add_button = \
    tk.Button(text='add to list',
              command=lambda:add_labelvalue_tolist(display_label['text']))
add_button.grid(column=0, row=1)
ROOT.mainloop()

答案 1 :(得分:0)

尝试创建类似这样的功能

def letterused():

converter=(letter.get())< letter is a entry box at the bottom is the code

converted.set(converter)

for i in range(1):

used_letters1.append(converter)<list

letter = ttk.Entry(root, width = 20,textvariable = letter)

letter.pack()