【问题标题】:Python 2.7 Tkinter CodePython 2.7 Tkinter 代码
【发布时间】:2016-11-06 05:59:53
【问题描述】:

我的 Tkinter 代码没有显示任何问题,但是当我运行它时,什么都没有显示。怎么了?

我使用的是 Python 2.7。顺便说一句,应该是比萨店游戏

这是我的代码:

import Tkinter as tk
from Tkinter import StringVar
import ttk
from random import randint , choice ,uniform

class Window(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        for F in (StartPage, Easy, Hard):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class StartPage():
    tk.Label(text = "Welcome!" , font = ("Verdana", 12)).pack()
    ttk.Button(text = "Play" , command = lambda: Window().show_frame(Play()))

class Play():
    def loop():
        q = randint(0, 10)
        p = uniform(1.50 , 10.50)
        t = uniform(.1, .9)
        p2 = p*q
        p3 = p2*t
        g = StringVar()
        ttk.Entry(textvariable=g).pack()
        ttk.Label(text = "Price Per Pizza: " + str(p) + " Amount: " + str(q) + " Tax: " + str(t))
        if g == p3 : ttk.Label(text = "Correct!")
        elif g != p3: ttk.Label(text = "Try Again.")
    loop()
    ttk.Button(text = "Play" , command = lambda: loop())

Window.mainloop

【问题讨论】:

  • Window.mainloop 不是函数调用。您应该在此处包含相关代码。
  • Window.mainloop 无处可寻,因为您的 Window 类中没有名为 mainloop 的函数。请考虑阅读How to ask a good question 并编辑您的帖子。
  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误在问题本身中重现它所需的最短代码。没有明确的问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable Example
  • @mwormser Window 是Tkinter.Tk 的子类,所以有mainloop 方法可用。它只是没有按应有的方式调用,w = Window(); w.mainloop()

标签: python python-2.7 tkinter tk


【解决方案1】:

这没有任何作用,因为它只是对 Window 类的 mainloop 方法的引用

Window.mainloop

你需要在调用mainloop之前创建一个Window类的实例

w = Window()
w.mainloop()

【讨论】:

  • 谢谢,我认为不需要它现在正在工作。
猜你喜欢
  • 2015-06-09
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 2013-04-11
  • 2023-04-05
  • 2017-12-08
  • 1970-01-01
相关资源
最近更新 更多