Python QtGui 日历小部件调用按钮

标签 python calendar widget qwidget qtgui

我正在使用 Python 的日历小部件。我需要在单击按钮时调用小部件。 情况是我找不到日历类中显示小部件本身的方法是什么。 日历类取自这里: 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)

据我所知,我可以只设置按钮的命令来调用位于日历类中的小部件显示方法。 如何获取每次单击按钮时显示日历小部件的方法?所有显示的都没有显示该小部件。

使用Python 3.3.5 蜘蛛 WinPython 3.3.5

**编辑**

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

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)

当我按下按钮时,它会打开日历窗口,但它是空的。控制台给我错误:

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

谢谢

最佳答案

没那么容易。问题是你混合了两个 GUI 库。因此,(至少)需要两个主要事件循环:一个用于 Tkinter 代码,另一个用于 PyQt 代码。
一种实现您想要的方法的方法 - 使用 subprocessthreading 模块在不同的线程中运行 calendar.py。示例:

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 主事件循环(肮脏的解决方案):

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()

编辑:如何调用小部件。 首先看this回答,并按照kalgasnik的建议修改您的ttkcalendar.py。然后试试这个:

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()

编辑2.解决问题
好吧,看来我发现了所有问题。

  1. 实际上,您导入了两次相同的模块 - 标准 calendar模块:

    from calendar import Calendar
    import calendar
    

但是您没有从ttkcalendar导入类Calendar模块(不要忘记按照描述进行更改 here )。 因此,导入应该如下所示:

import ttkcalendar
import calendar

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

ttkcal = ttkcalendar.Calendar(root2,firstweekday=calendar.SUNDAY)
  • 在您的代码中,主窗口初始化了两次:
    第 15 行:master = Tk()
    第 960 行:root = Tk()
    您需要删除第一个初始化。

  • 您可以在同一个主窗口中混合使用 pack()grid()。根据docs ,这是一个坏主意:

  • 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(列=0,行=0,粘性=(W,E))

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

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

    您的脚本经过描述的修改:
    https://gist.github.com/anonymous/65cb808dc64e414c0c12

    关于Python QtGui 日历小部件调用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25508319/

    相关文章:

    Python - 对试图理解的人的基本解释(CSV,Excel)

    python - 按字母顺序比较字符串 django db

    flutter - Flutter-无限容器

    widget - flutter 错误 : "Widget cannot build because is already in the process of building"

    xml - Inputmask 小部件不适用于 odoo 10 c

    python - 在 Pandas 查询中使用列表

    python - 在 Dash 应用程序上使用 Plotly 图表覆盖整个屏幕

    date - 尝试确定星期几是每月第一天

    Javascript框架日历插件

    javascript - 如何创建 html 表格周 View ? (日历)