使用Tkinter Python单击激活功能

时间:2018-07-10 11:18:23

标签: python python-3.x tkinter nmap

我有一个创建单选按钮的功能:

def scan_networks():
    net_scan = nmap.PortScanner()
    net_scan.scan(hosts='10.0.0.0/24', arguments='-sn')
    network_scan_window = Tk()
    for i, host in enumerate(net_scan.all_hosts()):
        targetable_hosts_label = Label(text="Targetable Hosts:").grid(row=0)
        hosts_radio = Radiobutton(network_scan_window, value=host, text=host).grid(row=1, column=i)
    network_scan_window.mainloop()

当我单击主机的其中一个主机电台时,我需要调用一个函数。

因此,可以说该函数返回了一堆具有不同ip的单选按钮 我需要使用单选按钮的值的参数调用函数x

1 个答案:

答案 0 :(得分:3)

使用Radiobuttons时,应使用variable将单选按钮组合在一起并跟踪当前值。您可以使用单选按钮的command选项在进行选择时调用一个函数,并获取变量的值以获取当前选择:

import tkinter as tk

def callback():
    print(v.get())

root = tk.Tk()

list = ['one', 'two', 'three']

v = tk.StringVar()
v.set(list[0])

for value in list:
    tk.Radiobutton(root, value=value, text=value, variable=v, command=callback).pack()

root.mainloop()