generator()
function, which is passed the amount
argument: def generator(amount): for n in range(amount): # , 4 a = randint(0,255) b = randint(0,255) c = randint(0,255) d = randint(0,255) # f = open('ip-addresses.txt', 'a', encoding='utf-8') f.write(str(a)+'.'+str(b)+'.'+str(c)+'.'+str(d)+'\n') f.close() # ? print('Success!')
from random import randint
, so as not to clutter up the namespace).Entry
widget), a button ( Button
widget), preferably a log output field, and add the name of the program so that it is clear at first glance what it is for: from tkinter import * # root = Tk() label1=Label(root, text=" ip-") label1.grid() # Frame, frame = Frame(root) frame.grid() label2=Label(frame, text=':') label2.grid(row=1,column=1) # entry_amount = Entry(frame, width=4, borderwidth=5) entry_amount.grid(row=1,column=2) # button1 = Button(frame, text="") button1.grid(row=1, column=3, padx=(10,0)) # output = Text(frame, bg="lightblue", font="Arial 9", width=45, height=3) output.grid(row=2, columnspan=8) root.mainloop()
def handler(): try: # .get() amount = int(entry_amount.get()) generator(amount) except ValueError: notif(" ") # , def notif(value): output.delete("0.0","end") # output.insert("0.0",value)
def notif(value)
, to which the argument-record is passed, in order to simplify the code when writing the functional, when it is necessary to display other errors. Also add to the description of the widget button1
method with the value of our function handler command=handler
(without parentheses at the end). button1 = Button(frame, text="", command=handler)
command
method, it may be necessary to set the from distutils import command
into imports. That's all, in fact, the program performs its main task.def delete()
: def delete(): try: remove('ip-addresses.txt') inserter(" ip-addresses.txt ") except: inserter(" ")
delete
: button2 = Button(frame, text=" ", command=delete) button2.grid(row=1, column=4, padx=(10,0))
frame
field. Do not forget to add the line from os import remove
to imports. Everything, with the "Delete" button sorted out.generator
function: def generator(amount, port=''): for n in range(amount): a = randint(0,255) b = randint(0,255) c = randint(0,255) d = randint(0,255) f = open('ip-addresses.txt', 'a', encoding='utf-8') f.write(str(a)+'.'+str(b)+'.'+str(c)+'.'+str(d)+port+'\n') f.close() # prc , n amount , amount — 100% prc = int(n//(amount/100)) print(str(prc)+'%') print('Success!') # GUI txt- notif("IP- \nip-addresses.txt")
def generator(amount, port=''): prc_bfr=0 # for n in range(amount): a = randint(0,255) b = randint(0,255) c = randint(0,255) d = randint(0,255) f = open('ip-addresses.txt', 'a', encoding='utf-8') f.write(str(a)+'.'+str(b)+'.'+str(c)+'.'+str(d)+port+'\n') f.close() prc = int(n//(amount/100)) #, prc if(prc!=prc_bfr): print(str(prc)+'%') prc_bfr = prc print('Success!') notif("IP- \nip-addresses.txt")
Radiobutton
widgets that will define two modes: generation with ports and no ports. You should also add the Entry
widget to enter the port: label2 = Label(frame, text=' :') label2.grid(row=2,column=1) entry_port = Entry(frame, width=4, borderwidth=5, state=DISABLED) entry_port.grid(row=2,column=2) # var1 radiobutton' var1 = IntVar() check_port1 = Radiobutton(frame, text=' ', variable=var1, value=1, command=lock) check_port1.grid(row=2,column=3) check_port2 = Radiobutton(frame, text=" ", variable=var1, value=0, command=lock) check_port2.grid(row=2,column=4)
entry_port
by default. Each check_port
performs a lock()
function when activated, which hides / activates the ports input field accordingly. We describe the lock
function: def lock(): # check_port2: if var1.get() == 1: entry_port.configure(state=NORMAL) # check_port1: elif var1.get() == 0: entry_port.configure(state=DISABLED)
handler
function, which is executed depending on the generation mode (on the value of var1
): def handler(): try: amount = int(entry_amount.get()) # if var1.get() == 1: port = ':'+str(int(entry_port.get())) # int , str, generator(amount, port) else: # — generator(amount) except ValueError: notif(" /")
def generator(amount, port='')
port
argument directly in the function declaration, we made it optional, and it will change its value when calling the function in the generation mode with ports. We also add it when writing to a text file: f.write(str(a)+'.'+str(b)+'.'+str(c)+'.'+str(d)+port+'\n')
import tkinter import random from tkinter import * from random import randint from os import remove from distutils import command print('Logs terminal:') def generator(amount, port=''): prc_bfr=0 for n in range(amount): a = randint(0,255) b = randint(0,255) c = randint(0,255) d = randint(0,255) f = open('ip-addresses.txt', 'a', encoding='utf-8') f.write(str(a)+'.'+str(b)+'.'+str(c)+'.'+str(d)+port+'\n') f.close() prc = int(n//(amount/100)) if(prc!=prc_bfr): print(str(prc)+'%') prc_bfr = prc print('Success!') notif("IP- \nip-addresses.txt") def notif(value): output.delete("0.0","end") output.insert("0.0",value) def handler(): try: amount = int(entry_amount.get()) if var1.get() == 1: port = ':'+str(int(entry_port.get())) generator(amount, port) else: generator(amount) except ValueError: notif(" /") def delete(): try: remove('ip-addresses.txt') notif(" ip-addresses.txt ") except: notif(" ") def lock(): if var1.get() == 1: entry_port.configure(state=NORMAL) elif var1.get() == 0: entry_port.configure(state=DISABLED) root = Tk() label1=Label(root, text=" ip-") label1.grid() frame = Frame(root) frame.grid() label2=Label(frame, text=':') label2.grid(row=1,column=1) entry_amount = Entry(frame, width=4, borderwidth=5) entry_amount.grid(row=1,column=2) button1 = Button(frame, text="", command=handler) button1.grid(row=1, column=3, padx=(10,0)) button2 = Button(frame, text=" ", command=delete) button2.grid(row=1, column=4, padx=(10,0)) label2=Label(frame, text=' :') label2.grid(row=2,column=1) entry_port = Entry(frame, width=4, borderwidth=5, state=DISABLED) entry_port.grid(row=2,column=2) var1 = IntVar() check_port1 = Radiobutton(frame, text=' ', variable = var1, value=1, command=lock) check_port1.grid(row=2,column=3) check_port2 = Radiobutton(frame, text=" ", variable = var1, value=0, command=lock) check_port2.grid(row=2,column=4) output = Text(frame, bg="lightblue", font="Arial 9", width=45, height=3) output.grid(row=3, columnspan=8) root.mainloop()
Source: https://habr.com/ru/post/339124/
All Articles