Custom Module in Window

122 Views Asked by At

I was wondering How I can get my code in a window instead of CMD When I run it outside of IDLE. I am using this code with a menu, which uses tkinter. Thanks in Advance. Also If You know how to shorten this code, Please Let me Know. Thanks!

def Castle ():
import random
repeat = "True"
RSN = random.randint(1, 6);

while (repeat == "True"):
    print("\nCastle:")
    print("\nThe Random Season Chosen is Season", RSN)
    if RSN == 1:
        print("and The Random Episode Chosen is Episode", random.randint(1, 10))
    elif RSN == 2:
        print("and The Random Episode Chosen is Episode", random.randint(1, 24))
    elif RSN == 3:
        print("and The Random Episode Chosen is Episode", random.randint(1, 24))
    elif RSN == 4:
        print("and The Random Episode Chosen is Episode", random.randint(1, 23))
    elif RSN == 5:
        print("and The Random Episode Chosen is Episode", random.randint(1, 24))
    elif RSN == 6:
        print("and The Random Episode Chosen is Episode", random.randint(1, 23))

    RSN = random.randint(1, 6);

    repeat = input ("\nDo You Want To Run Again?: ")


Castle ();
No = print ("\nPress Enter To Exit")
1

There are 1 best solutions below

1
On BEST ANSWER

This site isn't really for people to write complete programs for someone, but since you're learning, and apparently have a teacher that is also learning, I'll show you a complete working example.

Note: this is not the best way to write the program. It's not even the way I would write the program because I would take a more object-oriented approach. What I tried to do was write the simplest program possible.

import tkinter as tk
import random

# Define some data. For each series, define the number of 
# episodes in each season. For this example we're only defining
# one series. 
APP_DATA = {"Castle": [10, 24, 24, 23, 24, 23]}

# Define which element of APP_DATA we want to use
SERIES="Castle"

# Define a function that can update the display with new results
def update_display():
    max_seasons = len(APP_DATA[SERIES])
    season = random.randint(1, max_seasons)
    # Note: list indexes start at zero, not one. 
    # So season 1 is at index 0, etc
    max_episodes = APP_DATA[SERIES][season-1]
    episode = random.randint(1, max_episodes)

    text.delete("1.0", "end")
    text.insert("insert", "%s:\n" % SERIES)
    text.insert("insert", "The Random Season Chosen is Season %s\n" % str(season))
    text.insert("insert", "The Random Episode Chosen is Episode %s\n" % str(episode))

# Create a root window
root = tk.Tk()

# Create a text widget to "print" to:
text = tk.Text(root, width=40, height=4)

# Create a button to update the display
run_button = tk.Button(root, text="Click to run again", command=update_display)

# Arrange the widgets on the screen
text.pack(side="top", fill="both", expand=True)
run_button.pack(side="bottom")

# Display the first random values
update_display()

# Enter a loop, which will let the user click the button 
# whenever they want
root.mainloop()