Window 中的自定义模块

Custom Module in Window

我想知道当我在 IDLE 之外 运行 时,如何在 window 而不是 CMD 中获取我的代码。我将此代码与使用 tkinter 的菜单一起使用。提前致谢。另外,如果您知道如何缩短此代码,请告诉我。谢谢!

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")

这个站点并不是真正供人们为某人编写完整的程序,但由于您正在学习,而且显然有一位也在学习的老师,我将向您展示一个完整的工作示例。

注意:这不是编写程序的最佳方式。这甚至不是 编写程序的方式,因为我会采取更多 object-oriented approach。我试图做的是编写尽可能简单的程序。

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()