क्या कोई मानक तरीका tkinter ऐप्स उपयोगकर्ता को दिनांक चुनने की अनुमति देता है?मैं टिंकर में डेट पिकर कैसे बना सकता हूं?
उत्तर
नहीं, रुपये में दिनांक पिकर विजेट शामिल नहीं है। अजगर कैलेंडर विजेट के एक जोड़े आप की कोशिश कर सकते हैं: नहीं
http://svn.python.org/projects/sandbox/trunk/ttk-gsoc/samples/ttkcalendar.py
धन्यवाद मैं इसे देख लूंगा। मेरा सवाल था - क्योंकि उनके पास विजेट नहीं है - अधिकांश टीके/टिंकर डेवलपर्स ने क्या किया? बस तीन स्पिन बक्से हैं? अलग-अलग महीनों के बारे में क्या? लीप साल के बारे में क्या? – MKaras
एक पूर्व-टीसीएल/टीके डेवलपर के रूप में बोलते हुए, मैंने बस अपनी जरूरतों को कुछ बार घुमाया। वे टीसीएल (और पायथन) के दिनांक दिनचर्या के बाद से बनाने के लिए काफी छोटे हैं जो आपको डेटा खिला सकते हैं। आपको बस इतना करना है कि बटन या ग्रिड का ग्रिड बनाएं। –
यूप, वही यहाँ; टीसीएल/टीके (और टिंकर) नए विजेट बनाने के लिए इसे बहुत छोटा बनाते हैं। यदि आप कुछ संरचना चाहते हैं तो विजेट निर्माण किट देखें (उपरोक्त लिंक किए गए डब्लूकेसी कैलेंडर द्वारा उपयोग किया जाता है)। –
, लेकिन आप .. एक स्वरूपित स्ट्रिंग से एक datetime तत्व के रूप में उपयोगकर्ता से प्राप्त कर सकते हैं
उदाहरण:
import datetime
userdatestring = '2013-05-10'
thedate = datetime.datetime.strptime(userdatestring, '%Y-%m-%d')
http://docs.python.org/2/library/datetime.html#strftime-strptime-behavior देखें। यह आसान है, हालांकि तारीख प्राप्त करने का सबसे उपयोगकर्ता अनुकूल तरीका नहीं है।
जहां तक मुझे मिल सकता था। किसी को जो भविष्य में ऐसा करने के लिए करना चाहता है के लिए:
मैं एक CalendarDialog बनाने के लिए (this अतः पद से साथ संशोधन) tkSimpleDialog और ttkcalendar.py इस्तेमाल किया। तीन फाइलें मेरे github पर उपलब्ध हैं।
import Tkinter
import ttkcalendar
import tkSimpleDialog
class CalendarDialog(tkSimpleDialog.Dialog):
"""Dialog box that displays a calendar and returns the selected date"""
def body(self, master):
self.calendar = ttkcalendar.Calendar(master)
self.calendar.pack()
def apply(self):
self.result = self.calendar.selection
# Demo code:
def main():
root = Tkinter.Tk()
root.wm_title("CalendarDialog Demo")
def onclick():
cd = CalendarDialog(root)
print cd.result
button = Tkinter.Button(root, text="Click me to see a calendar!", command=onclick)
button.pack()
root.update()
root.mainloop()
if __name__ == "__main__":
main()
हे, आपके द्वारा लिंक की गई पाइथन स्रोत फ़ाइल में वास्तव में उन संशोधनों में नहीं है - एक ठोकर ब्लॉक में भाग गया जब तक मुझे एहसास हुआ कि इसे वास्तविक टिंकर ऐप के साथ अच्छा खेलने के लिए संशोधित किया जाना था: पी –
हाँ, यही कारण है कि मैं अपने github पर भी पैच संस्करणों से जुड़ा हुआ है। – Moshe
आह होचा, धन्यवाद। मैंने बस यह माना कि सभी लिंक एक ही स्थान पर गए थे क्योंकि मैंने उन्हें पर्याप्त रूप से पढ़ने के लिए पर्याप्त नहीं पढ़ा था कि वे सभी जिथब नहीं थे: पी –
यह है कि हम कैसे tkinter में एक अजगर datepicker कोड है:
नीचे मेरी CalendarDialog.py में कोड है।
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Rambarun Komaljeet
# License: Freeware
# ---------------------------------------------------------------------------
import calendar
import tkinter as tk
import time
from tkinter import ttk
class MyDatePicker(tk.Toplevel):
"""
Description:
A tkinter GUI date picker.
"""
def __init__(self, widget=None, format_str=None):
"""
:param widget: widget of parent instance.
:param format_str: print format in which to display date.
:type format_str: string
Example::
a = MyDatePicker(self, widget=self.parent widget,
format_str='%02d-%s-%s')
"""
super().__init__()
self.widget = widget
self.str_format = format_str
self.title("Date Picker")
self.resizable(0, 0)
self.geometry("+630+390")
self.init_frames()
self.init_needed_vars()
self.init_month_year_labels()
self.init_buttons()
self.space_between_widgets()
self.fill_days()
self.make_calendar()
def init_frames(self):
self.frame1 = tk.Frame(self)
self.frame1.pack()
self.frame_days = tk.Frame(self)
self.frame_days.pack()
def init_needed_vars(self):
self.month_names = tuple(calendar.month_name)
self.day_names = tuple(calendar.day_abbr)
self.year = time.strftime("%Y")
self.month = time.strftime("%B")
def init_month_year_labels(self):
self.year_str_var = tk.StringVar()
self.month_str_var = tk.StringVar()
self.year_str_var.set(self.year)
self.year_lbl = tk.Label(self.frame1, textvariable=self.year_str_var,
width=3)
self.year_lbl.grid(row=0, column=5)
self.month_str_var.set(self.month)
self.month_lbl = tk.Label(self.frame1, textvariable=self.month_str_var,
width=8)
self.month_lbl.grid(row=0, column=1)
def init_buttons(self):
self.left_yr = ttk.Button(self.frame1, text="←", width=5,
command=self.prev_year)
self.left_yr.grid(row=0, column=4)
self.right_yr = ttk.Button(self.frame1, text="→", width=5,
command=self.next_year)
self.right_yr.grid(row=0, column=6)
self.left_mon = ttk.Button(self.frame1, text="←", width=5,
command=self.prev_month)
self.left_mon.grid(row=0, column=0)
self.right_mon = ttk.Button(self.frame1, text="→", width=5,
command=self.next_month)
self.right_mon.grid(row=0, column=2)
def space_between_widgets(self):
self.frame1.grid_columnconfigure(3, minsize=40)
def prev_year(self):
self.prev_yr = int(self.year_str_var.get()) - 1
self.year_str_var.set(self.prev_yr)
self.make_calendar()
def next_year(self):
self.next_yr = int(self.year_str_var.get()) + 1
self.year_str_var.set(self.next_yr)
self.make_calendar()
def prev_month(self):
index_current_month = self.month_names.index(self.month_str_var.get())
index_prev_month = index_current_month - 1
# index 0 is empty string, use index 12 instead,
# which is index of December.
if index_prev_month == 0:
self.month_str_var.set(self.month_names[12])
else:
self.month_str_var.set(self.month_names[index_current_month - 1])
self.make_calendar()
def next_month(self):
index_current_month = self.month_names.index(self.month_str_var.get())
try:
self.month_str_var.set(self.month_names[index_current_month + 1])
except IndexError:
# index 13 does not exist, use index 1 instead, which is January.
self.month_str_var.set(self.month_names[1])
self.make_calendar()
def fill_days(self):
col = 0
# Creates days label
for day in self.day_names:
self.lbl_day = tk.Label(self.frame_days, text=day)
self.lbl_day.grid(row=0, column=col)
col += 1
def make_calendar(self):
# Delete date buttons if already present.
# Each button must have its own instance attribute for this to work.
try:
for dates in self.m_cal:
for date in dates:
if date == 0:
continue
self.delete_buttons(date)
except AttributeError:
pass
year = int(self.year_str_var.get())
month = self.month_names.index(self.month_str_var.get())
self.m_cal = calendar.monthcalendar(year, month)
# build dates buttons.
for dates in self.m_cal:
row = self.m_cal.index(dates) + 1
for date in dates:
col = dates.index(date)
if date == 0:
continue
self.make_button(str(date), str(row), str(col))
def make_button(self, date, row, column):
"""
Description:
Build a date button.
:param date: date.
:type date: string
:param row: row number.
:type row: string
:param column: column number.
:type column: string
"""
exec(
"self.btn_" + date + " = ttk.Button(self.frame_days, text=" + date
+ ", width=5)\n"
"self.btn_" + date + ".grid(row=" + row + " , column=" + column
+ ")\n"
"self.btn_" + date + ".bind(\"<Button-1>\", self.get_date)"
)
def delete_buttons(self, date):
"""
Description:
Delete a date button.
:param date: date.
:type: string
"""
exec(
"self.btn_" + str(date) + ".destroy()"
)
def get_date(self, clicked=None):
"""
Description:
Get the date from the calendar on button click.
:param clicked: button clicked event.
:type clicked: tkinter event
"""
clicked_button = clicked.widget
year = self.year_str_var.get()
month = self.month_str_var.get()
date = clicked_button['text']
self.full_date = self.str_format % (date, month, year)
print(self.full_date)
# Replace with parent 'widget' of your choice.
try:
self.widget.delete(0, tk.END)
self.widget.insert(0, self.full_date)
except AttributeError:
pass
if __name__ == '__main__':
def application():
MyDatePicker(format_str='%02d-%s-%s')
root = tk.Tk()
btn = tk.Button(root, text="test", command=application)
btn.pack()
root.mainloop()
असंभव, क्योंकि टिंकर बहुत कम है। यदि आप इसे स्वयं के निर्माण के बिना कुछ फैंसी चाहते हैं, तो आपकी सबसे अच्छी शर्त बड़ी, बैटरी-शामिल जीयूआई टूलकिट्स में से एक है (मैं क्यूटी की सिफारिश करता हूं)। – delnan
+1 पूरी तरह से उचित सवाल है। यह यूआई चीज की तरह है जिसे टूलकिट के भीतर मानकीकृत किया जाना चाहिए। आश्चर्यचकित टीके इसे पेश नहीं करता है। – Anne