Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 标签Tkinter的更新问题_Python_Windows_Button_Tkinter_Label - Fatal编程技术网

Python 标签Tkinter的更新问题

Python 标签Tkinter的更新问题,python,windows,button,tkinter,label,Python,Windows,Button,Tkinter,Label,我真的被困在一个基本的问题上:我有这个代码 from tkinter import * import sys import subprocess import tkinter as tk def cd(): f=(subprocess.check_output("net view")) e=(f.decode(sys.stdout.encoding)) label1=Label(text=e).pack() def mainscreen(): ma

我真的被困在一个基本的问题上:我有这个代码

from tkinter import *
import sys

import subprocess

import tkinter as tk

def cd():  
    f=(subprocess.check_output("net view"))
    e=(f.decode(sys.stdout.encoding))
    label1=Label(text=e).pack()


def mainscreen(): 
    mainscreen=Tk()
    mainscreen.title("Terfysgol's kit V 2.0")
    frame1=Frame(mainscreen)
    frame1.pack()
    puls1=Button(frame1,text="List of device", borderwidth= "2",command= cd).pack()

mainscreen()

当我一直按按钮运行它时,它会创建一个新标签,但我只想更新标签1的文本。

这就是您想要的:

def cd():

    f=(subprocess.check_output("net view"))
    e=(f.decode(sys.stdout.encoding))
    label1.config(text = e)
然后在导入后的程序顶部,您需要放置:

label1 = Label()
label1.pack()
请注意,我并不是说这是一个很好的程序结构,但这取决于您的选择。这个答案只是一个快速解决方案,可以为您提供足够的信息,以解决您需要的其他问题


您还可以删除导入tkinter,因为tk行已经导入了tkinter。

1。不要链接几何体方法。您在frame1上做得正确,但在label1和puls1上做得不正确。2.每次你按下按钮,它都会创建一个新标签,因为是你告诉它的。首先在主屏幕中创建标签,然后让cd简单地用label1.configtext=e.uhm更新它现在我有以下情况:在mainscreen中,我有label1=Labelmainscreen.pack,但当我按下puls1时,它会给我一个错误,表明cd中没有定义label1。然后在函数外部的安全位置创建label1,避免垃圾收集。同样,不要链接几何体方法。