正在加载数据库?

发布于 2024-10-23 12:20:27 字数 426 浏览 3 评论 0原文

我的问题是当我连接到 MySQL 数据库时如何执行某些操作? 为了更清楚地说明这一点:让我们说我尝试连接到数据库:

db = MySQLdb.connect(host = "testhost",user ="testuser", passwd ="testpw", db = "testdb")

嗯,一般来说,当主机不是本地主机时,它通常需要一些时间来加载,当这种情况发生时,应用程序“冻结”(我正在使用 wxPython)。现在,我想要的是,它不会显示“冻结”,而是显示代表“加载”的东西,它可以是图像,文本,没关系,而不是冻结。另外,闪屏怎么样?据我尝试,我所做的只是一些闪屏,这些闪屏在 X 次后以及当我点击它时消失。我真的不知道如何利用它来加载资源,例如MySQL数据库。上次我尝试使用闪屏加载 MySQL 数据库时,它实际上先加载,然后显示闪屏(笑)。 谢谢。

my question is how can I do something while I connect to a MySQL database?
To make this clearer: Lets say I try to connect to a database:

db = MySQLdb.connect(host = "testhost",user ="testuser", passwd ="testpw", db = "testdb")

Well, thing is, generally, when the host isn't localhost, it usually takes up a bit to load and while this happens, the application "freezes" (I'm using wxPython). Now, what I want is, instead of "freezing", it would display something that represents "loading", it could be an image, a text, doesn't matter, instead of freezing. Also, what about splashscreens? As far as I tried, all I've managed to do is some splashscreens which disappear after X time and when I click on it. I really don't know how to take advantage of it to load resources and, for example, a MySQL database. The last time I tried to load up a MySQL database with splashscreen, it actually loaded up first, then it showed the splashscreen (lol).
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧夏天 2024-10-30 12:20:27

感谢您的帮助,我得到了一个答案,一个针对 wxPython 的更具体的答案。我按照这个简单的例子使它工作:

import wx
import thread
from time import sleep

class MainFrame(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        self.label = wx.StaticText(self, label="Ready")
        self.btn = wx.Button(self, label="Start")
        self.gauge = wx.Gauge(self)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.label, proportion=1, flag=wx.EXPAND)
        sizer.Add(self.btn, proportion=0, flag=wx.EXPAND)
        sizer.Add(self.gauge, proportion=0, flag=wx.EXPAND)

        self.SetSizerAndFit(sizer)

        self.Bind(wx.EVT_BUTTON, self.onButton)

    def onButton(self, evt):
        self.btn.Enable(False)
        self.gauge.SetValue(0)
        self.label.SetLabel("Running")
        thread.start_new_thread(self.longRunning, ())

    def onLongRunDone(self):
        self.gauge.SetValue(100)
        self.label.SetLabel("Done")
        self.btn.Enable(True)

    def longRunning(self):
        """This runs in a different thread.  Sleep is used to simulate a long running task."""
        sleep(3)
        wx.CallAfter(self.gauge.SetValue, 20)
        sleep(5)
        wx.CallAfter(self.gauge.SetValue, 50)
        sleep(1)
        wx.CallAfter(self.gauge.SetValue, 70)
        sleep(10)
        wx.CallAfter(self.onLongRunDone)

if __name__ == "__main__":
    app = wx.PySimpleApp()
    app.TopWindow = MainFrame(None)
    app.TopWindow.Show()
    app.MainLoop()

从这里: http://wiki.wxpython.org/LongRunningTasks ,希望这有帮助:)。

Thanks for all your help, I came to an answer, a more concrete one for wxPython. I followed this simple example to make it work:

import wx
import thread
from time import sleep

class MainFrame(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        self.label = wx.StaticText(self, label="Ready")
        self.btn = wx.Button(self, label="Start")
        self.gauge = wx.Gauge(self)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.label, proportion=1, flag=wx.EXPAND)
        sizer.Add(self.btn, proportion=0, flag=wx.EXPAND)
        sizer.Add(self.gauge, proportion=0, flag=wx.EXPAND)

        self.SetSizerAndFit(sizer)

        self.Bind(wx.EVT_BUTTON, self.onButton)

    def onButton(self, evt):
        self.btn.Enable(False)
        self.gauge.SetValue(0)
        self.label.SetLabel("Running")
        thread.start_new_thread(self.longRunning, ())

    def onLongRunDone(self):
        self.gauge.SetValue(100)
        self.label.SetLabel("Done")
        self.btn.Enable(True)

    def longRunning(self):
        """This runs in a different thread.  Sleep is used to simulate a long running task."""
        sleep(3)
        wx.CallAfter(self.gauge.SetValue, 20)
        sleep(5)
        wx.CallAfter(self.gauge.SetValue, 50)
        sleep(1)
        wx.CallAfter(self.gauge.SetValue, 70)
        sleep(10)
        wx.CallAfter(self.onLongRunDone)

if __name__ == "__main__":
    app = wx.PySimpleApp()
    app.TopWindow = MainFrame(None)
    app.TopWindow.Show()
    app.MainLoop()

From here: http://wiki.wxpython.org/LongRunningTasks , hope this helps :).

旧人 2024-10-30 12:20:27

如果您的程序需要同时做两件事,那么使用多线程是一个好方法。就您而言,您有一个想要保持响应能力的 GUI,并且有一个要连接的数据库。您需要在单独的线程中完成数据库工作,而不是在处理 GUI 事件的线程上;这将使 GUI 在建立连接时保持工作。恐怕我无法告诉您如何用 Python 编写多线程代码,但我认为 Google 是您的朋友。

If your program needs to do two things at once, then using multiple threads is a good way to do it. In your case, you have a GUI that you'd like to stay responsive, and you have a database to connect to. You need to do the database work in a separate thread, instead of on the thread that handles GUI events; that will keep the GUI working while the connection is being made. I'm afraid I can't tell you how to write multithreaded code in Python, but I think Google is your friend here.

叫思念不要吵 2024-10-30 12:20:27

有关如何使用 threading 模块编写多线程 Python 程序的示例,请参阅

http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/1/

和官方文档:

http://docs.python.org/library/threading.html

For examples of how to write multithreaded Python programs using the threading module, see

http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/1/

and the official documentation:

http://docs.python.org/library/threading.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文