【问题标题】:How to append value from a tkinter entry to list without apostophe如何将 tkinter 条目中的值附加到不带撇号的列表中
【发布时间】:2020-01-20 21:36:20
【问题描述】:

作为一个整体,我对编程和 python 非常陌生,可能有一个简单的解决方案可以解决我在谷歌搜索中找不到的问题。

所以,问题在于Spared_list 的定义。当我从我的条目spared 中获取值并将它们放入我的列表中时,它们中的每一个都会在它们周围用撇号保存。当我想检查Count_up 的值是否存在于列表中时,这是程序稍后出现的问题。

import subprocess
import tkinter as tk
from tkinter import*

sparedlist=[]

def Spared_list():
    print("reached spared list")
    sparedlist.append(spared.get())
    print(sparedlist)

def execute():
    Count_up=0
    while Count_up < 1000:
        if Count_up in sparedlist:
            Count_up=Count_up+1
            print("HI")
        else:
            print("VKSK",Count_up," shut down",sep='')
            Count_up = Count_up+1
Chaos = tk.Tk()
Chaos.title("remote shutdown")
tk.Label(Chaos, text="Enter number of spared device(s) (ex:123, if device=VKSK123)").pack()
spared = tk.Entry(Chaos)
spared.pack()
tk.Button(Chaos, text="add", activebackground="Green", command=Spared_list).pack()
tk.Button(Chaos, text="execute", background="Red", command=execute).pack()

我知道这可能不是编码和使用变量的正确方法,但我刚刚开始。

【问题讨论】:

    标签: python tkinter append tkinter-entry


    【解决方案1】:

    这是程序稍后在我想检查列表中是否存在 Count_up 的值时出现的问题。

    您的sparedlist 包含字符串,而您的Count_up 是一个整数。如果您想检查一个整数是否包含在字符串列表中,您必须首先将该整数转换为字符串。例如

    if str(Count_up) in sparedlist:
        ....
    

    或者,在将字符串添加到列表时将它们转换为整数:

        sparedlist.append(int(spared.get()))
    

    【讨论】:

      猜你喜欢
      • 2019-06-11
      • 2015-09-03
      • 2020-02-09
      • 2018-11-04
      • 2017-06-05
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      相关资源
      最近更新 更多