简体   繁体   English

是否可以使用 tkinter 获取标签变量并放入列表中

[英]Is it possible to grab a label variable and put into list with tkinter

I've created a temperature converter programme in which the calculated temperature from an entry widget gets displayed in a separate label, what I need to do is to grab that converted variable and put it into a list.我创建了一个温度转换器程序,其中来自条目小部件的计算温度显示在单独的标签中,我需要做的是获取转换后的变量并将其放入列表中。

I think that making a connected entry widget to the label widget would work where they are connected so I could grab the variable using the .get method but that would look awfully messy.我认为将连接的条目小部件连接到标签小部件可以在它们连接的地方工作,因此我可以使用 .get 方法获取变量,但这看起来非常混乱。 Is there any other way I could proceed with this?我还有其他方法可以继续这个吗?

This is my first post and I am a beginner in Python, very sorry if the code looks messy and if I included too much code.这是我的第一篇文章,我是 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()

A tk.Label displayed value can be accessed via the text property , labelwidgetname['text'].可以通过文本属性 labelwidgetname['text'] 访问 tk.Label 显示的值。 Depeneding on when and how you want the independent list of stored values to be updated there are a variety of options.根据您希望更新存储值的独立列表的时间和方式,有多种选择。 The example shows one if the user is required to press a submission button.该示例显示了用户需要按下提交按钮的情况。 This could be adapted, for example,when the tempreture calculation is performed.例如,这可以在执行温度计算时进行调整。

Of course it would be simpler to update the list of stored values directly at the point in the script where the calculated tempreture for the label text has been derived.当然,直接在脚本中已导出标签文本的计算温度的点处更新存储值列表会更简单。

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()

try making a function that is like this尝试制作一个这样的功能

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM