【问题标题】:Getting tkinter widget values outside main application class?在主应用程序类之外获取 tkinter 小部件值?
【发布时间】:2018-04-09 22:07:58
【问题描述】:

我有一个 pygame 项目,它有一个主菜单 - 在所述菜单中我有它,以便在单击 Options 按钮时触发 Tkinter UI 打开。 UI 有一个影响游戏速度的OptionMenu 小部件。如何在用于创建 Tkinter 窗口的类之外检索 OptionMenu 的值

这是一个简化的问题:

class GUI(object):
    def __init__(self):
        import Tk  # etc...
        # CODE FOR THE PROGRAM WINDOW
        defaultSpeed = StringVar(root)
        speedMenu = OptionMenu(root, defaultSpeed, 'Slow', 'Normal, 'Fast')
        speedMenu.pack()

我知道我需要defaultSpeed.get() 才能获得价值。

Pygame 菜单有这个:

click = pygame.mouse.get_pressed()
clicked = click[0] == 1
if "Play" clicked:
   startGame(ticks)
if "Options" clicked:
   options = GUI()

此时^^^我如何以某种方式获得速度(defaultSpeed.get())并将其作为全局变量,以便我可以在startGame函数中使用它来影响时钟的滴答数?

想一想,我本质上是在问如何在初始化 Tkinter 类时定义一个全局变量?

我尝试在 GUI() 中创建一个名为 getSpeed 并调用 options.getSpeed() 的函数,但由于某种原因,我仍然有一个逻辑错误,这意味着它从未真正打印/显示/返回。

我知道你不应该在 __init__ 中返回值 - 我应该移动我所有的 Tkinter 应用程序。到GUI() 中的一个函数并将__init_ 留在外面?当然我可以避免初始化它吗?通过 OOP 程序化编程会更好吗(允许我在主菜单中简单地调用 GUI())?

感谢任何帮助解决问题。

问题回顾:

  1. 主菜单有 2 个选项(1 个在 Pygame 中运行游戏,1 个在 Tkinter 中运行选项窗口)
  2. 我需要从选项菜单中获取选项并将其应用于游戏中的变量。

编辑:我被要求提供最少的代码 我目前真的很忙,但这里是最少的伪代码 - 您需要做的就是为游戏窗口和菜单创建尺寸(它们都是 940,500)

import pygame
import time
import math
import tkinter
from tkinter import *
pygame.init()

clock = pygame.tick.Clock()
ticks = 0

#Here is all the game code

#This function is the main game and includes the game loop
def StartGame(ticks):
    running = True
    myGame1 = teams()
    myGame1.startTeam()
    #Here is some functions providing the rules of the sport
    def updateBall(ticks):
        theBall.x += float(theBall.speed) * ticks / 1000
    while running:
        myGame1.displayPlayers()
        updateBall(ticks)
        #Calling alot of functions and the game running is here
        ticks = clock.tick(30)
        pygame.display.flip
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                pygame.quit()
            quit

def game_intro(menu_image):
    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                pygame.quit()
                quit
        click = pygame.mouse.get_pressed()
        clicked = click[0] == 1

        if 62+288 > mouse[0] > 62 and 150+28 > mouse[1] > 150:
            if clicked:
                startGame(ticks)
                pass

        elif 62+288 > mouse[0] > 62 and 230+30 > mouse[1] > 230:
            if clicked:
                options = GUI()

class GUI(object):
    def __init__(self):
        root = self.root = tkinter.Tk
        root.geometry('500x400')
        root.configure(background = '#ffffff')
        speedLabel = tkinter.Message(root, text = 'Game speed: ')

        defaultSpeed = StringVar(root)
        speedMenu = OptionMenu(root,defaultSpeed, 'Slow', 'Normal', 'Fast')

        speedLabel.grid(row = 0, column = 0)
        speedMenu.grid(row = 0, column = 1)

game_intro(menu_image)

【问题讨论】:

  • 您的代码 sn-ps 有点稀疏。请edit您的问题并在其中添加足够的内容以创建一个最小的、可运行的示例,我们可以使用它来为您测试解决方案。听起来您使用的是 Python 2.x。无论如何,请添加一个指示正确版本的标签。
  • 恕我直言,我相信我所包含的内容足以让某人模拟一个类似的小程序 - 我现在真的很忙,但已经模拟了一个几乎相同的小程序符合我更大项目的逻辑。如果您认为您需要更多,请告诉我 - 本质上,问题需要有人在 Tkinter 中创建一个小选项屏幕,并在一个简单的 Pygame 主菜单窗口中调用它。您甚至不需要主游戏,但我已将其包含在编辑后的伪代码中。
  • 为什么每个想帮助你的人都必须模仿一些东西?提出问题的人需要做更多的工作......
  • 所有需要做的就是创建两个窗口——我已经包含了示例所需的主要部分。没有人有义务做任何事情,但我在接下来的 20 分钟内有很多事情要做——作为一名学生——我不能坐下来争论这个。老实说,我非常感谢您的 cmets,我不希望帮助创建模型,有人可能会通过查看代码 sn-ps 知道答案。谢谢,罗文
  • 添加了一个答案解释

标签: python-3.x oop tkinter pygame logic


【解决方案1】:

在创建defaultSpeed之前:

global defaultSpeed

然后在你访问它之前:

global defaultSpeed

这基本上意味着您可以在使用global之后从任何地方访问该变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2016-11-26
    相关资源
    最近更新 更多