Tkinter 窗口中的自定义模块

Tkinter 窗口中的自定义模块,tkinter,window,python-3.1,Tkinter,Window,Python 3.1,我想知道当我在空闲之外运行代码时,如何在窗口中而不是CMD中获取代码。我使用这个代码和一个菜单,它使用tkinter。提前谢谢。另外,如果你知道如何缩短这个代码,请让我知道。谢谢 def Castle (): import random repeat = "True" RSN = random.randint(1, 6); while (repeat == "True"): print("\nCastle:") print("\nThe Random Season Chosen

我想知道当我在空闲之外运行代码时,如何在窗口中而不是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")

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

注意:这不是编写程序的最佳方式。这甚至不是我写程序的方式,因为我会花更多的时间。我试图做的是尽可能编写最简单的程序

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

第一步是学习tkinter教程。我在高中上过Python课。我的老师被难倒了,所以他把我带到这里……我相信你一定会为此感到沮丧。然而,给出一个有用的答案意味着完全为您编写程序,因为实际上您完全没有tkinter代码。GUI程序和控制台应用程序本质上是不同的,它们的工作方式截然不同(即:GUI使用事件循环并响应事件,控制台应用程序按顺序运行)。对不起,也许我不清楚,我希望能够在窗口中打印,而不是在CMD中。上面没有tkinter代码,因为我不确定这是在窗口中打印的最佳方法。当我尝试时,它无法正常工作,但我可以让基本的打印语句正常工作。只是导入和随机化程序让我搞砸了。另外,我并不想发疯,因为我非常渴望了解您是否希望保留确切的程序结构,而只是在窗口中显示文本?或者你想写一个合适的图形用户界面,给用户一个按钮,而不是问他们一个问题?布莱恩,我和我的老师谢谢你!我有很多其他的节目要做,我可以用它作为模板!如果有什么我能做的,请告诉我!