-1

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")
ringmikey
  • 3
  • 2
  • 1
    Step 1 would be to work through a tkinter tutorial. – Bryan Oakley Apr 06 '15 at 21:01
  • I have, I take a Python Class at My High School. My Teacher is stumped so he directed me here... – ringmikey Apr 06 '15 at 22:01
  • I'm sure you must be frustrated by that. However, to give a useful answer would mean to completely write the program for you, because as it is you have absolutely zero tkinter code. GUI programs and console applications are fundamentally different things that work in significantly different ways (ie: a GUI uses an event loop and responds to events, a console app runs sequentially) – Bryan Oakley Apr 06 '15 at 22:10
  • Sorry, Maybe I wasn't clear, I want to be able to make this printed in a window, not CMD. There is no tkinter code above because I am not sure its the best method for printing this in a window. When I try it doesn't seam to work, yet I can get basic Print Statements to work. It's just the imports and and randomizer that is causing me to mess up. Also, I don't mean to come off mad, as I am very eager to learn – ringmikey Apr 06 '15 at 22:37
  • Do you want to keep the exact program structure, but simply display the text in a window? Or do you want to write a proper GUI that gives the user a button rather than asking them a question? – Bryan Oakley Apr 07 '15 at 14:55
  • I would like a proper GUI for our program, but I am not sure how to do that, is it possible for you to give me a simple example that I could modify with my code...? Once again Thank You a Ton, I am learning a lot! – ringmikey Apr 07 '15 at 15:04

1 Answers1

0

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()
Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Bryan, Me and My Teacher Thank You! I have tons of Other Shows to Do and I can use this as a Template! If there's anything I can do, Let me Know! – ringmikey Apr 07 '15 at 16:31