簡體   English   中英

將def打印到tkinter window,什么時候按下按鈕?

[英]print def into tkinter window ,when push button?

當我按下按鈕時,我如何使用我的 def "main" 或 "CheckIP(listIP)" 將其打印到我的 tkinter window...? 可能需要輸入我的控制台信息,例如“ping 地址”

然后我在控制台:

192.168.0.90 不活動 192.168.0.10 不活動 192.168.0.12 不活動 192.168.88.1 不活動- 我想在我的 tkinter Z05B8C74CBD96FBF2DE4C1A352702FFF4 中查看此信息

請幫助:(

這是我的代碼:

#Modules
from tkinter import *
from tkinter import messagebox
from tkinter import simpledialog
import os
import platform
import subprocess


#Parameters for window
root = Tk()
root.title("Пинг МКУ")
root.geometry("300x200")
root.resizable(width=False, height=False)
root['bg'] = '#ccc'


#Main function for ping our devices
def main():
    listIP = list()
    OpenFile(listIP)
    CheckIP(listIP)
#Open txt file with number's
def OpenFile(listIP):
    File = open('ipping.txt', 'r', encoding='utf-8')
    for IP in File:
        listIP.append(IP[:-1])
    File.close()

    return listIP

#Chek our adresses
def CheckIP(listIP):
     with open('ipping.txt', 'r') as f:
        for ip in f:
            result = subprocess.Popen(["ping", "-c", "1", "-n", "-W", "2", ip], stdout=f, stderr=f).wait()
            if result:
                return ip, "inactive"
            else:
                return ip, "active"

#Event
#secondary_but_ping = Checkbutton ( text = 'Пинговать каждые 5 минут')
Ping = Button( text = 'Пингануть',
               font = 'consolas 13',
               bg = '#48494f',
               fg = '#eff5c9',
               activeforeground = '#eff5c9',
               activebackground = '#6e6f73',
               width = '22',
               height = '2',
               command = main)

textVar = StringVar()
Ping_1 = Label( textvariable = textVar )
Ping_1.pack()

def button1():
    temp = textVar.get()
    textVar.set( temp + "\n" + CheckIP('listIP') ) # "\n" is newline character


Ping.bind('<Button-1>', button1)

root.mainloop()

main()

感謝 K. Stergiou 解決了問題,但現在我遇到了 tkinter 的問題,我需要 ping 我所有的 txt 地址,但現在只 ping 1 個地址

Tkinter window

CheckIP()中,您將字符串打印到控制台,但您需要返回它們以便它們可以在 Ping_1 中使用。 看一下這個:

def CheckIP(listIP):
     with open('ipping.txt', 'r') as f:
        for ip in f:
            result = subprocess.Popen(["ping", "-c", "1", "-n", "-W", "2", ip], stdout=f, stderr=f).wait()
            if result:
                return ip + "inactive" # string concatenation
            else:
                return ip + "inactive"

為了 ping 多個地址,創建一個字符串變量 (tkinter.StringVar()) 並使用它來更改Ping_1 label 的內容:

textVar = StringVar()
textVar.set("example placeholder")
Ping_1 = Label( textvariable = textVar )

def button1():
    temp = textVar.get()
    textVar.set( temp + "\n" + CheckIP('listIP') ) # "\n" is newline character
    Ping_1.pack()

Ping.bind('<Button-1>', button1)

我忘記了每次更改textVar變量時都需要.pack() Ping_1

您不應在CheckIP() function 內的 for 循環中調用return ,因為它會在第一次調用 subprocess.Popen subprocess.Popen(...)后終止 function 。

您應該將單個 ping 結果保存到列表中,並使用結果列表更新 label。

此外,您不應同時使用command選項和bind() ,因為分配給這些選項的回調將在單擊Ping按鈕時一起執行。

以下是基於您的簡化示例:

from tkinter import *
import subprocess

#Parameters for window
root = Tk()
root.title("Пинг МКУ")
root.geometry("300x200")
root.resizable(width=False, height=False)
root['bg'] = '#ccc'


#Chek our adresses
def CheckIP():
    results = []
    with open('ipping.txt', 'r') as f:
        for ip in f.read().splitlines():
            rv = subprocess.Popen(["ping", "-c", "1", "-n", "-W", "2", ip]).wait()
            results.append(ip+(" inactive" if rv else " active"))
    textVar.set('\n'.join(results))

Button(text='Пингануть',
       font='consolas 13',
       bg='#48494f',
       fg='#eff5c9',
       activeforeground='#eff5c9',
       activebackground='#6e6f73',
       width=22,
       height=2,
       command=CheckIP).pack()

textVar = StringVar()
Label(textvariable=textVar).pack(fill=X)

root.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM