简体   繁体   English

Tkinter 日历没有在正确的位置网格

[英]Tkinter Calendar doesn't grid at the right place

So i'm working at minimal application that contains a DateEntry, but im having trouble at griding the widget at the right place, it should be packed inside of the container that's holding other inputs, but actually it's been grided at the bottom of the page.因此,我正在使用包含 DateEntry 的最小应用程序,但我无法在正确的位置网格化小部件,它应该被打包在包含其他输入的容器内,但实际上它已在页面底部网格化.

Below there's the actual code of the view.下面是视图的实际代码。 Im not understanding why this is happening, but i think that's probably something with the DateEntry class that im inhereting in class MyDateEntry(DateEntry) .我不明白为什么会这样,但我认为这可能与我在class MyDateEntry(DateEntry)中继承的 DateEntry class 有关。

import tkinter as tk
from tkinter import ttk
from tkcalendar import DateEntry
from datetime import date


class View(tk.Tk):
    PAD = 10

    LABELS_LIST = [
        'Selecione o estado: ',
        'Digite o municipio: ', 
        'Selecione o dia: ',
        'Digite a temperatura máxima: ',
        'Digite a temperatura minima: '
    ]

    STATE_LIST = [
        'AC',
        'AL',
        'AP',
        'AM',
        'BA',
        'CE',
        'DF',
        'ES',
        'GO',
        'MA',
        'MT',
        'MS',
        'MG',
        'PA',
        'PB',
        'PR',
        'PE',
        'PI',
        'RJ',
        'RN',
        'RS',
        'RO',
        'RR',
        'SC',
        'SP',
        'SE',
        'TO'
    ]

    def __init__(self, controller):
        super().__init__()
        self.controller = controller
        self.title('EToCalc1.0')
        self.temp_max_var = tk.StringVar()
        self.temp_min_var = tk.StringVar()
        self._change_style()
        self._make_main_frm()
        self._make_labels()
        self._make_combobox_state()
        self._make_city_entry()
        self._make_calendar()
        self._make_max_temp_entry()

    def main(self):
        self.mainloop()

    def _make_main_frm(self):
        self.main_frm = ttk.Frame(self)
        self.main_frm.pack(padx=self.PAD, pady=self.PAD)

    def _make_city_entry(self):
        frm = ttk.Frame(self.outer_frm)
        frm.grid()
        self.city_entry = ttk.Entry(frm)
        self.city_entry.pack(pady=self.PAD, padx=self.PAD)

    def _make_calendar(self):
        frm = ttk.Frame(self.outer_frm)
        frm.grid()
        self.de = MyDateEntry(frm)
        self.de.pack(pady=self.PAD, padx=self.PAD)

    def _change_style(self):
        style = ttk.Style(self)
        style.theme_use('clam')

    def _make_labels(self):
        frm = ttk.Frame(self.main_frm)
        frm.grid(row=0,column=0)
        i = 0
        for item in self.LABELS_LIST:
            label = ttk.Label(frm, text=item)
            label.grid(row=i, column=0, padx=self.PAD,pady=self.PAD)
            i += 1

    def _make_combobox_state(self):
        self.outer_frm = ttk.Frame(self.main_frm)
        self.outer_frm.grid(row=0, column=1)
        frm = ttk.Frame(self.outer_frm)
        frm.grid()
        self.state_combobox = ttk.Combobox(frm, values=self.STATE_LIST)
        self.state_combobox.pack(pady=self.PAD, padx=self.PAD)
        self.state_combobox.set(value='Escolha um estado')

    def _make_max_temp_entry(self):
        frm = ttk.Frame(self.outer_frm)
        frm.grid()
        temp_max_entry = ttk.Entry(frm, textvariable=self.temp_max_var)
        temp_min_entry = ttk.Entry(frm, textvariable=self.temp_min_var)
        temp_max_entry.pack(padx=self.PAD, pady=self.PAD)
        temp_min_entry.pack(padx=self.PAD, pady=self.PAD)
        


class MyDateEntry(DateEntry):
    def __init__(self, master=None, **kw):
        DateEntry.__init__(self, master=None, **kw)
        # add black border around drop-down calendar
        self._top_cal.configure(bg='black', bd=1)
        # add label displaying today's date below
        tk.Label(self._top_cal, bg='gray90', anchor='w',
            text='Hoje: %s' % date.today().strftime('%d/%m/%Y')).pack(fill='x')

There's the printscreen of the window, note that the DateEntry is at the bottom有 window 的打印屏幕,注意 DateEntry 在底部

Thank you!谢谢!

SOLUTION解决方案

Thanks to @jasonharper who commented the solution, as quoted:感谢@jasonharper,他对解决方案发表了评论,引用如下:

DateEntry.__init__(self, master=None, **kw) - you are throwing away the master parameter being passed to your class, and instead explicitly passing None to the superclass (making the widget a child of the root window instead).> DateEntry.__init__(self, master=None, **kw) - 您正在丢弃传递给 class 的主参数,而是显式地将 None 传递给超类(使小部件成为根 window 的子级)。>

So i just removed the =None from master=None and the correct frame is passing as argument.所以我只是从master=None中删除了=None None 并且正确的帧作为参数传递。

Thank you @jasonharper!谢谢@jasonharper!

You are explicitly forcing the widget to be in the root window with master=None in this line of code:在这行代码中,您明确强制小部件位于根 window 中, master=None

DateEntry.__init__(self, master=None, **kw)

Instead, you should be passing in the value of master that was passed in by the caller:相反,您应该传入调用者传入的master值:

DateEntry.__init__(self, master, **kw)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM