Home > Net >  Hi, in new to python and i want to make a GUI for my python script using tkinter
Hi, in new to python and i want to make a GUI for my python script using tkinter

Time:08-31

So I'm trying to make a GUI for my python script with Tkinter. I want I fairly simple just window where you can type the IP you want to ping and a button to execute the command. Here is my script:

import os
address = input("Enter Ip Here:")
os.system(f"ping {address} -t -l 65500")

So basically what I want is to be able to type the {address} in a small window and then press a button below to execute this but I can't figure out how to do this. (I have already tried the script and it works in the cmd prompt. I just want a GUI for it. Help would be appreciated!

I also made a separate script which opens a window with Tkinter and it has an exit button to close the window:

import Tkinter as tk
from Tkinter import ttk

# root window
root = tk.Tk()
root.geometry('600x600')
root.resizable(False, False)
root.title('Ping Of Death')

# exit button
exit_button = ttk.Button(
    root,
    text='Execute',
    command=lambda: root.quit()
)

exit_button.pack(
    ipadx=5,
    ipady=5,
    expand=True
)

root.mainloop()

CodePudding user response:

In order to do this you need to add an Entry to your window and execute your ping with address being set to the value of the entry once your button is pressed. Here is a simple example on how this could work:

import tkinter as tk


def ping(address):
    import os
    os.system(f"ping {address} -t -l 65500")


root = tk.Tk()
root.geometry('600x600')
root.resizable(False, False)
root.title('Ping Of Death')

ip_input_label = tk.Label(root, text="IP: ")
ip_input_label.grid(row=0, column=0)

ip_input = tk.Entry(root)
ip_input.grid(row=0, column=1)

button = tk.Button(root, text="Ping", command=lambda: ping(ip_input.get()))
button.grid(row=1, column=0)

root.mainloop()

CodePudding user response:

Try this:

import tkinter as tk
from tkinter import ttk
import os

def startx():
    #address = input("Enter Ip Here:")
    os.system(f"ping {text.get()} -t -l 65500")

# root window
root = tk.Tk()
root.geometry('600x600')
root.resizable(False, False)
root.title('Ping Of Death')

var = tk.StringVar 

text = tk.Entry(root,textvariable=var, width = 25)
text.pack()

# exit button
exit_button = ttk.Button(
    root,
    text='Execute',
    command=startx
)

exit_button.pack(
    ipadx=5,
    ipady=5,
    expand=True
)

root.mainloop()

Output: enter image description here

  • Related