Python QtGui日历小部件调用按钮 [英] Python QtGui Calendar Widget Call button

查看:472
本文介绍了Python QtGui日历小部件调用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python的日历小部件。我需要在点击按钮时调用小部件。
情况是,我找不到在日历类中显示小部件本身的方法是什么。
日历类取自这里:
http ://www.eurion.net/python-snippets/snippet/Calendar_Date%20picker.html

I am using a calendar widget for Python. And I need to call the widget when a button is clicked. The situation is that I cannot find what is the method in the calendar class that displays the widget itself. The calendar class was taken from here: http://www.eurion.net/python-snippets/snippet/Calendar_Date%20picker.html

以下是我的汇入:

from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import time
import requests #needs to be installed
import pymysql #needs to be installed
import csv 
import win32com.client #needs to be installed
from calendar import Calendar
import datetime

以下是按钮创建:

# Calendar Buttons
calBut=ttk.Button(f2, width=4, text="Cal",       command=Calendar.what_method?).grid(column=3,row=1, sticky=W)

据我所知,我可以设置按钮的命令调用位于日历类中的窗口小部件显示方法。
如何获取每次我的按钮被点击时显示日历小部件的方法?

As far as I know, I can just set the command of the button to call the widget display method located in the calendar class. How to get the method that displays the calendar widget each time my button is clicked? None of the ones showing are displaying the widget.

使用Python 3.3.5
Spider
WinPython 3.3.5

Using Python 3.3.5 Spider WinPython 3.3.5

** EDIT **

程序有选项卡,f2表示按钮的选项卡。

The program has tabs and the f2 indicates the tab where the button will be.

from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import time
import requests #needs to be installed
import pymysql #needs to be installed
import csv 
import win32com.client #needs to be installed
import datetime
from calendar import Calendar
import calendar


#################################
# Create Button Click Calendar

 def callback():
    root2=Toplevel(f2)
    ttkcal = Calendar(root2,firstweekday=calendar.SUNDAY)
    ttkcal.pack(expand=1, fill='both')
    root2.update()
    root2.minsize(root2.winfo_reqwidth(), root2.winfo_reqheight())



 # Calendar Buttons

 b=ttk.Button(f2, width=4, text="Cal", command=callback).grid(column=3,row=1, sticky=W)

打开日历窗口,但它是空的。并且控制台给我错误:

When I press the button, it opens the calendar window, but it is empty. And the console gives me error:

TypeError: __init__() got multiple values for argument 'firstweekday

谢谢

推荐答案

太简单。问题是你混合两个GUI库。因此,需要两个主要事件循环(至少):一个用于 Tkinter 代码,一个用于 PyQt 代码。

使用 subprocess 线程模块运行 calendar.py 在不同的线程。示例:

Not so easy. The problem is that you mix the two GUI libraries. Therefore it is necessary two main event loops (at least): one for Tkinter code and one for PyQt code.
One way to do what you want - using subprocess and threading modules to run calendar.py in different thread. Example:

from tkinter import *
from tkinter import ttk

import subprocess
import threading

master = Tk()

def callback():
    subprocess.call('python calendar.py')


b=ttk.Button(master, width=4, text="Cal", command=lambda:threading.Thread(target=callback).start()).grid(column=3,row=1, sticky=W)


mainloop()

另一种方式 - 在回调函数中创建Qt主要事件循环(脏解决方案):

Another way - creating Qt main event loop inside callback function (dirty solution):

from tkinter import *
from tkinter import ttk
from calendar import Calendar
import sys
from PyQt4 import QtGui

master = Tk()

def callback():
    app = QtGui.QApplication(sys.argv)
    gui = Calendar()
    gui.show()
    app.exec_()


b=ttk.Button(master, width=4, text="Cal", command=callback).grid(column=3,row=1, sticky=W)

mainloop()

编辑:如何调用小部件。 ,请查看答案,并将您的 ttkcalendar.py 修改为 kalgasnik 建议。然后尝试这样:

How to call widget. First of all, look at this answer, and modify your ttkcalendar.py as kalgasnik suggested. Then try this:

from tkinter import *
from tkinter import ttk
from ttkcalendar import Calendar
import calendar

master = Tk()

def callback():
    root2=Toplevel(master)
    ttkcal = Calendar(root2,firstweekday=calendar.SUNDAY)
    ttkcal.pack(expand=1, fill='both')
    root2.update()
    root2.minsize(root2.winfo_reqwidth(), root2.winfo_reqheight())

b=ttk.Button(master, width=4, text="Cal", command=callback).grid(column=3,row=1, sticky=W)

mainloop()

EDIT 2.解决问题

好​​,看来我发现了所有问题。

EDIT 2. Solving the problems
Ok, it seems I found all problems.


  1. 实际上,标准日历模块:

from calendar import Calendar
import calendar


但您不导入类日历来自 ttkcalendar 模块(不要忘记按照
此处所述进行更改)。
所以,导入应该看起来像这样:

But you do not import the class Calendar from ttkcalendar module (Do not forget to change it as described here). So, import should look like this:

import ttkcalendar
import calendar

创建日历(为了清晰起见,我更改了一些代码):

Creating calendar (I changed the code a bit for clarity):

ttkcal = ttkcalendar.Calendar(root2,firstweekday=calendar.SUNDAY)




  1. 在您的代码中,主窗口初始化两次:

    第15行: master = Tk

    行960: root = Tk()

    您需要删除第一次初始化。

  1. In your code, the main window is initialized twice:
    line 15: master = Tk()
    line 960: root = Tk()
    You need to remove the first initialization.

您搭配 pack() grid $ c>在同一个主窗口。根据文档,这是一个坏主意:

You mix pack() and grid() in the same master window. According the docs, it is a bad idea:




警告:切勿在同一主窗口中混合网格和打包。 Tkinter
将很乐意花你的一生的余下的时间尝试谈判一个
解决方案,两个经理都很满意。而不是等待,杀死
的应用程序,并再次看看你的代码。一个常见的错误是
是对一些小部件使用错误的父代。

Warning: Never mix grid and pack in the same master window. Tkinter will happily spend the rest of your lifetime trying to negotiate a solution that both managers are happy with. Instead of waiting, kill the application, and take another look at your code. A common mistake is to use the wrong parent for some of the widgets.

> nb.pack(fill ='both',expand ='yes')你必须写这样的东西
nb.grid(column = 0,row = 0,sticky =(W,E))

So, instead nb.pack(fill='both', expand='yes') you have to write something like this nb.grid(column=0, row=0, sticky=(W, E))

最后,这里是指向固定代码的链接:


ttkcalendar.py (已修改,可供使用):
https://gist.github.com/anonymous/5e0d973f57e185572df2


您的脚本具有以下修改:

https://gist.github.com/anonymous/65cb808dc64e414c0c12

Finally, here are links to the fixed code:

ttkcalendar.py (already modified, ready to use):
https://gist.github.com/anonymous/5e0d973f57e185572df2

Your script with described modifications:
https://gist.github.com/anonymous/65cb808dc64e414c0c12

这篇关于Python QtGui日历小部件调用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆