【问题标题】:wxpython buttons blocked/not clickingwxpython 按钮被阻止/不点击
【发布时间】:2018-03-05 02:28:17
【问题描述】:

点击不会触发事件。

我正在为python中的简单数据可视化制作一个简单的GUI

似乎有什么东西挡住了按钮和选择,实际上是主面板中的任何东西。

它正在工作,但后来我从 matplotlib 中放入了图表 我怀疑这与展示位置/盒子大小有关,但我一生都无法弄清楚。

非常感谢任何帮助。

import sys
from random import *
import signal
import sqlite3
from datetime import date, datetime
import wx
import wx.lib.mixins.inspection as WIT
import matplotlib
from numpy import arange, sin, pi

matplotlib.use('WX')
from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx

from matplotlib.figure import Figure
class myGui(wx.Frame):
    def __init__(self,parent,id,title):
        wx.Frame.__init__(self,parent,id,title)
        self.parent = parent
        self.initialise()
        pnl = wx.Panel(self)


        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        t = arange(0.0, 3.0, 0.01)
        s = sin(2 * pi * t)

        self.axes.plot(t, s)
        self.canvas = FigureCanvas(self, -1, self.figure)

        self.sizer = wx.BoxSizer(wx.HORIZONTAL)


        rightMenuBtns = wx.BoxSizer(orient=wx.VERTICAL)

        self.sizer.Add(rightMenuBtns, proportion = 1, flag=wx.LEFT | wx.ALL, border = 5)
        self.sizer.Add(self.canvas, 1, wx.RIGHT | wx.TOP | wx.EXPAND)
        fileMenu = wx.Menu()
        loadFile = fileMenu.Append(wx.ID_OPEN)
        exitItem = fileMenu.Append(wx.ID_EXIT)

        helpMenu = wx.Menu()
        aboutItem = helpMenu.Append(wx.ID_ABOUT)

        self.problems = wx.Choice(pnl,choices = allProblems())
        rightMenuBtns.Add(self.problems, proportion = 1, flag=wx.CENTER | wx.ALL, border = 5)

        self.solutions = wx.Choice(pnl,choices = allSolutionsText(allProblems()[self.problems.GetSelection()]))
        rightMenuBtns.Add(self.solutions, proportion = 1, flag=wx.CENTER | wx.ALL, border = 5)

        solve = wx.Button(pnl, label="SOLVE")
        rightMenuBtns.Add(solve, proportion = 1, flag=wx.CENTER | wx.ALL, border = 5)

        menuBar = wx.MenuBar()
        menuBar.Append(fileMenu, "&File")
        menuBar.Append(helpMenu, "&Help")

        self.SetMenuBar(menuBar)

        self.Bind(wx.EVT_MENU, self.OnExit,  exitItem)
        self.Bind(wx.EVT_MENU, self.OnAbout, aboutItem)
        self.Bind(wx.EVT_MENU, self.loadFile, loadFile)
        self.Bind(wx.EVT_BUTTON, self.Solve, solve)
        self.solutions.Bind(wx.EVT_CHOICE, self.Solutions)
        self.problems.Bind(wx.EVT_CHOICE, self.Problems)

        self.dlg = wx.TextEntryDialog(pnl, 'Enter the amount of seconds you want to run solver for:','Time')

        self.SetSizer(self.sizer)
        self.Fit()

    def initialise(self):
        self.Show(True)

    def OnExit(self, event):
        self.Close(True)

    def OnAbout(self, event):
        wx.MessageBox("Hello",
        "Made By Thomas Csere",                                      wx.OK | wx.ICON_INFORMATION)

    def loadFile(self, event):
        openFileDialog = wx.FileDialog(self, "Open", "", "",
                                        "Traveling Salesman Problem (*.tsp)|*.tsp",
                                        wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        openFileDialog.ShowModal()
        addProblem(openFileDialog.GetPath())
        wx.MessageBox("Problem Added")
        openFileDialog.Destroy()

    def Problems(self,event):
        print("woo")
        self.solutions.Clear()
        self.solutions.AppendItems(allSolutionsText(allProblems()[self.problems.GetSelection()]))

    def Solutions(self,event):
        print("yay")

    def Solve(self,event):

        self.dlg.ShowModal()
        if(self.problems.GetSelection()>=0):
            solveFull(allSolutionsText(allProblems()[self.problems.GetSelection()], self.dlg.GetValue()))



app = wx.App()
frame = myGui(None,-1,"My Application")
app.MainLoop()

【问题讨论】:

  • 我怀疑您使用编辑器对该代码执行了错误的find and replace。您的代码中完全缺少许多函数/变量。您需要先解决这个问题,然后才能让任何人弄清楚它到底出了什么问题。

标签: python python-3.x matplotlib wxpython wxwidgets


【解决方案1】:

您从不将面板添加到 sizer,因此它可能与 FigureCanvas 重叠。

此外,最好不要长时间创建并保持对话。只需根据需要创建它们以显示它们,然后在获取它们的值后Destroy

【讨论】:

    猜你喜欢
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多