在这段代码中,我试图指责来自域的用户返回他们的凭据,但是当我实际上在变量pep中调用finger命令并将其显示在变量结果中时会被打包到tkinter窗口中。当我运行它时,唯一显示的是'0',我可以看到finger命令正常运行并返回正确的信息后台调试I / O但它没有在tkinter窗口中显示它,我不知道为什么。有帮助吗? (P.S.对于运行代码的任何人,在finger_target上输入一个空白字符串会返回所有在线用户的列表)
import tkinter,subprocesses,time
from tkinter import *
root=Tk()
finger_target,fingerlabel=Entry(root),Label(root, text="TARGET: ")
finger_confirm=Button(root, text="FINGER", command=lambda: TheActualfinger(finger_target.get()))
finger_target.grid(row=0, column=1)
fingerlabel.grid(row=0,column=0)
finger_confirm.grid(row=0, column=2)
def TheActualfinger(user):
pep=subprocess.call(("finger",finger_target.get()+"@telehack.com"))
outfinger= Tk()
def windowCalsDEFAULT():
w,h =500, 500
ws, hs=outfinger.winfo_screenwidth(),outfinger.winfo_screenheight()
x, y=(ws/2) - (w/2),(hs/2) - (h/2)
outfinger.geometry('%dx%d+%d+%d' % (w, h, x, y))
results= Label(outfinger, text=pep).pack(fill="x")
outfinger.mainloop()
root.mainloop()
答案 0 :(得分:1)
subprocess.call仅返回状态代码 - finger
返回的错误代码 - 而0
表示OK
。
使用subprocess.check_output获取文字。
BTW:tkinter
应该只有一个Tk()
窗口(主窗口)。要创建其他窗口,请使用Toplevel()
。 tkinter
应该只有一个mainloop
(它是"engine"
)
顺便说一句:您可以使用tkinter.messagebox.showinfo
来显示带有消息的窗口。
import tkinter as tk
import tkinter.messagebox
import subprocess
# --- functions ---
def window_with_result(text):
win = tk.Toplevel()
w = 500
h = 500
ws = win.winfo_screenwidth()
hs = win.winfo_screenheight()
x = (ws-w)/2
y = (hs-h)/2
win.geometry('%dx%d+%d+%d' % (w, h, x, y))
tk.Label(win, text=text, justify='left').pack(fill="x")
def actual_finger(user):
result = subprocess.check_output(["finger", user+"@telehack.com"])
print('[DEBUG]:', result)
# message window
tkinter.messagebox.showinfo(user, result)
# or own window
window_with_result(result)
# --- main ---
root = tk.Tk()
target = tk.Entry(root)
label = tk.Label(root, text="TARGET: ")
confirm = tk.Button(root, text="FINGER", command=lambda:actual_finger(target.get()))
target.grid(row=0, column=1)
label.grid(row=0, column=0)
confirm.grid(row=0, column=2)
root.mainloop()
BTW:我使用lower_case
名称来使代码更具可读性。