【问题标题】:Custom Module in Window窗口中的自定义模块
【发布时间】:2015-04-06 18:40:30
【问题描述】:

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

【问题讨论】:

  • 第 1 步是完成 tkinter 教程。
  • 我有,我在高中上过 Python 课。我的老师被难住了,所以他把我带到了这里......
  • 我相信你一定为此感到沮丧。然而,给出一个有用的答案意味着完全为你编写程序,因为你的 tkinter 代码绝对为零。 GUI 程序和控制台应用程序是根本不同的东西,它们以明显不同的方式工作(即:GUI 使用事件循环并响应事件,控制台应用程序按顺序运行)
  • 对不起,也许我不清楚,我希望能够在窗口中打印,而不是 CMD。上面没有 tkinter 代码,因为我不确定它是在窗口中打印它的最佳方法。当我尝试它并不能正常工作时,我可以让基本的打印语句正常工作。只是导入和随机化器导致我搞砸了。另外,我并不是要发疯,因为我非常渴望学习
  • 您想保持准确的程序结构,而只是在窗口中显示文本吗?或者您想编写一个适当的 GUI,为用户提供一个按钮而不是向他们提问?

标签: tkinter window python-3.1


【解决方案1】:

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

注意:这不是编写程序的最佳方式。这甚至不是 编写程序的方式,因为我需要更多的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()

【讨论】:

  • 布莱恩,我和我的老师,谢谢!我还有很多其他节目要做,我可以用它作为模板!如果有什么我能做的,请告诉我!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 2010-11-19
  • 1970-01-01
相关资源
最近更新 更多