使用存储的变量时可以保持tkinter窗口打开吗?

问题描述 投票:-1回答:1

在Jupyter笔记本中,我使用tkinter收集用户输入,以便其他代码可以生成输出文件。我已经创建了一个提交按钮来存储变量,但是在其余的代码能够使用它们并运行到输出之前,必须关闭tkinter窗口。

我希望能够一直运行到输出文件,然后输入新输入而无需关闭并重新启动。如何使tkinter窗口始终保持打开状态?


from tkinter import *
import datetime
from datetime import timedelta
from dateutil.relativedelta import relativedelta
import pandas as pd

class MyWindow:
    def __init__(self, win):
        self.lbl=Label(win, text="Tool")
        self.lbl1=Label(win, text='ndays')
        self.lbl2=Label(win, text='Date 1')

        self.t1=Entry()
        self.t2=Entry()

        self.lbl.place(x=150, y=10)

        self.lbl1.place(x=50, y=50)
        self.t1.place(x=150, y=50)

        self.lbl2.place(x=50, y=90)
        self.t2.place(x=150, y=90)    
        self.b1=Button(win, text='Submit', command=self.submit)

        self.b2=Button(win, text='Refresh')
        self.b2.bind('<Button-1>', self.refresh)

        self.b1.place(x=150, y=260)
        self.b2.place(x=270, y=260)

    def submit(self):
        global ndays, screen

        ndays=self.t1.get()
        screen=self.t2.get()

    #in case of refresh
    def refresh(self, event):
        self.t1.delete(0, 'end')
        self.t2.delete(0, 'end')

window=Tk()
mywin=MyWindow(window)
window.title("Tool")
window.geometry("400x400+10+10")
window.mainloop()

我必须退出tkinter窗口才能访问名称和屏幕日期变量。我希望能够刷新变量并将其再次运行到输出,而不必关闭窗口。

下面的代码是我将要创建自定义日历的下一步。


screen = datetime.datetime.strptime(screen, '%m/%d/%y').date()

# find the of the number of days backwards.
ndays=int(ndays)
thirty_back = screen + relativedelta(days=-ndays - 1)

delta1 = screen - thirty_back

# Empty lists to loop into
date_list = []  # dates
day_counter_thing = []
day_o_week = []  # real day of week
counter = []  # list for the days used

day_count = -1  # starts at -1 so that screen day can be 0

for i in range(delta1.days):
    day = screen - timedelta(days=i)  # know where to start the tlfb

    date_list.append(day)  # list of dates in the loop
    day_count = day_count + 1  # add a count for each day in the loop
    counter.append(day_count)  # keep a list of all the day counts

    day_o_week.append(day.strftime("%A"))  # add the real day of the week
    name = day.weekday()  # name of day by index of day in week
    day_counter_thing.append(name)  # keep the list
max_day = max(counter)  # gives you reference so that you can swap the counter after screen

# put the values from the loops into a dataframe
df = pd.DataFrame({'Date': date_list, "Day": day_o_week, "Counter": day_counter_thing, "TLFB_Day": counter})

python tkinter calendar global-variables jupyter
1个回答
0
投票

一旦在窗口上单击提交,此代码会将数据框打印到终端中。只要窗口没有关闭,mainloop就会运行,并且无法运行任何其他代码。

from tkinter import *
import datetime
from datetime import timedelta
from dateutil.relativedelta import relativedelta
import pandas as pd

class MyWindow:
    def __init__(self, win):
        self.lbl=Label(win, text="Tool")
        self.lbl1=Label(win, text='ndays')
        self.lbl2=Label(win, text='Date 1')

        self.t1=Entry()
        self.t2=Entry()

        self.lbl.place(x=150, y=10)

        self.lbl1.place(x=50, y=50)
        self.t1.place(x=150, y=50)

        self.lbl2.place(x=50, y=90)
        self.t2.place(x=150, y=90)    
        self.b1=Button(win, text='Submit', command=self.submit)

        self.b2=Button(win, text='Refresh')
        self.b2.bind('<Button-1>', self.refresh)

        self.b1.place(x=150, y=260)
        self.b2.place(x=270, y=260)

    def calendar( self, screen, ndays ):
        # find the of the number of days backwards.

        screen = datetime.datetime.strptime(screen, '%m/%d/%y').date()
        ndays=int(ndays)
        thirty_back = screen + relativedelta(days=-ndays - 1)

        delta1 = screen - thirty_back

        # Empty lists to loop into
        date_list = []  # dates
        day_counter_thing = []
        day_o_week = []  # real day of week
        counter = []  # list for the days used

        day_count = -1  # starts at -1 so that screen day can be 0

        for i in range(delta1.days):
            day = screen - timedelta(days=i)  # know where to start the tlfb

            date_list.append(day)  # list of dates in the loop
            day_count = day_count + 1  # add a count for each day in the loop
            counter.append(day_count)  # keep a list of all the day counts

            day_o_week.append(day.strftime("%A"))  # add the real day of the week
            name = day.weekday()  # name of day by index of day in week
            day_counter_thing.append(name)  # keep the list
        max_day = max(counter)  # gives you reference so that you can swap the counter after screen

        # put the values from the loops into a dataframe
        return pd.DataFrame({'Date': date_list, "Day": day_o_week, "Counter": day_counter_thing, "TLFB_Day": counter})

    def submit(self):
        print( self.calendar( self.t2.get(), self.t1.get()) )

    #in case of refresh
    def refresh(self, event):
        self.t1.delete(0, 'end')
        self.t2.delete(0, 'end')

window=Tk()
mywin=MyWindow(window)
window.title("Tool")
window.geometry("400x400+10+10")
window.mainloop()

一种替代方法是将结果显示在窗口本身中,而不是在文本对象或一系列标签中显示它们。

© www.soinside.com 2019 - 2024. All rights reserved.