How to Generate Combinations of the Component from Four Text Files using Python & tkinter

Posted

in

by

This program generates a random combination of components. This can be a fun program.
It creates the combination of all the components in the four list.

For example; if each list contains 5 words. Then the total number of combinations would be 5 x 5 x 5 x 5 = 625

Code

import random
import tkinter as tk
from tkinter import messagebox
import itertools
import webbrowser


# Create the GUI window
window = tk.Tk()
window.title("List Shuffler")

# Create the text boxes for file names
micros_textbox = tk.Entry(window, width=50)
micros_textbox.insert(0, "micros.txt")
micros_textbox.grid(row = 0, column = 0, pady = 5)

sensors_textbox = tk.Entry(window, width=50)
sensors_textbox.insert(0, "sensor.txt")
sensors_textbox.grid(row = 1, column = 0, pady = 5)

inputs_textbox = tk.Entry(window, width=50)
inputs_textbox.insert(0, "inputs.txt")
inputs_textbox.grid(row = 2, column = 0, pady = 5)

displays_textbox = tk.Entry(window, width=50)
displays_textbox.insert(0, "displays.txt")
displays_textbox.grid(row = 3, column = 0, pady = 5)

# Create the label for the result
result_label = tk.Text(window, height=10, width=50)
result_label.grid(row = 0, column = 1,rowspan = 5, pady = 5)

# Define the function to shuffle the lists
def shuffle_lists():
    # Open the first file and read in its contents
    with open(micros_textbox.get(), "r") as f:
        A = f.read().splitlines()

    # Open the second file and read in its contents
    with open(sensors_textbox.get(), "r") as f:
        B = f.read().splitlines()

    # Open the third file and read in its contents
    with open(inputs_textbox.get(), "r") as f:
        C = f.read().splitlines()

    with open(displays_textbox.get(), "r") as f:
        D = f.read().splitlines()

    # Shuffle the lists
    random.shuffle(A)
    random.shuffle(B)
    random.shuffle(C)
    random.shuffle(D)

    # Select one item from each list and combine them into a string
    result = A[0] + " + " + B[0] + " + " + C[0] + " + " + D[0] +"\n\n"

    # Update the label with the result
    result_label.insert(tk.INSERT,result)
def generate_combinations():
    # Open the first file and read in its contents
    with open(micros_textbox.get(), "r") as f:
        A = f.read().splitlines()

    # Open the second file and read in its contents
    with open(sensors_textbox.get(), "r") as f:
        B = f.read().splitlines()

    # Open the third file and read in its contents
    with open(inputs_textbox.get(), "r") as f:
        C = f.read().splitlines()

    with open(displays_textbox.get(), "r") as f:
        D = f.read().splitlines()

    # Get all the combinations of the lists
    combinations = list(itertools.product(A, B, C, D))

    # Write the combinations to a text file
    with open("combinations.txt", "w") as f:
        for combination in combinations:
            f.write(' + '.join(combination) + "\n")

    # Show a message box with the number of combinations generated
    messagebox.showinfo("Combinations Generated", f"{len(combinations)} combinations were generated and saved to combinations.txt.")


# Create the button to shuffle the lists
shuffle_button = tk.Button(window, text="Shuffle", command=shuffle_lists)
shuffle_button.grid(row = 4, column = 0, pady = 5)
# Add a button to generate the combinations
generate_button = tk.Button(window, text="Generate Combinations", command=generate_combinations)
generate_button.grid(row = 5, column = 0, pady = 5)

def open_website():
    webbrowser.open_new("http://www.exasub.com")

link_label = tk.Label(window, text="exasub.com", font=("Arial", 14), fg="blue", cursor="hand2")
link_label.grid(row=6, column=0, pady=10)
link_label.bind("<Button-1>", lambda event: open_website())
# Run the GUI
window.mainloop()

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *