0
点赞
收藏
分享

微信扫一扫

9-20|我生成的这个窗口,我怎么获取这个pid

要获取当前Python进程的进程ID(PID),您可以使用Python的`os`模块中的`getpid`方法。  


以下是您可以在前面的`tkinter`窗口代码中添加的内容,以显示当前进程的PID:


```python

import tkinter as tk

import os


def main():

   root = tk.Tk()


   # 获取屏幕宽度和高度

   screen_width = root.winfo_screenwidth()

   screen_height = root.winfo_screenheight()


   # 设置窗口的宽度和高度为屏幕的一半,并计算窗口的起始坐标,使其居中

   window_width = screen_width // 2

   window_height = screen_height // 2

   x = (screen_width - window_width) // 2

   y = (screen_height - window_height) // 2


   # 应用窗口的尺寸和位置

   root.geometry(f"{window_width}x{window_height}+{x}+{y}")


   # 获取并显示当前进程的PID

   pid = os.getpid()

   pid_label = tk.Label(root, text=f"当前进程ID: {pid}")

   pid_label.place(x=10, y=10)


   btn = tk.Button(root, text="关闭", command=root.quit)

   btn.place(x=window_width//2 - 30, y=window_height//2 - 15)


   root.mainloop()


if __name__ == "__main__":

   main()

```


在这个示例中,我们在窗口的左上角显示了当前进程的PID。您可以根据需要调整其位置或样式。

举报

相关推荐

0 条评论