【问题标题】:Passing List between Functions with Tkinter使用 Tkinter 在函数之间传递列表
【发布时间】:2013-12-17 16:27:02
【问题描述】:

我正在制作一个待办事项列表以获取乐趣,但我在将列表带到不同的功能时遇到了麻烦。该程序的目标是用户按下“新任务”按钮并输入一些内容,然后将字符串添加到列表中,然后通过列表框显示。这是我目前所拥有的。

P.S 忽略 DelTask​​ 函数,因为它仍然是 WIP

from Tkinter import *
import Tkinter
import tkMessageBox
import sys

count = 0

class ToDoList:

def __init__(self):

    #print testlist


    self._count = 0
    self.main_window = Tkinter.Tk()

    #size   
    self.main_window.minsize(1000,800)
    self.main_window.maxsize(1000,800)

    #frames
    self.title_frame = Tkinter.Frame(self.main_window)
    #self.side_frame = Tkinter.Frame(self.main_window)

    #labels
    self.title = Tkinter.Label(self.title_frame, text = 'To Do List', font = ("Purisa",30))
    self.title2 = Tkinter.Label(self.title_frame, text = 'By Kevin', font = ("Purisa",15))

    #buttons
    self.newtask_button = Tkinter.Button(self.main_window, text='New Task', command = self.NewTask, width=20)
    self.newtask_button.grid()

    self.deltask_button = Tkinter.Button(self.main_window, text='Delete Task', command = self.DelTask, width=20)
    self.deltask_button.grid()




    #execute
    self.title.pack(side='top')
    self.title2.pack(side='top')
    self.title_frame.pack(side='top')
    self.newtask_button.pack(padx=4, pady=4)
    self.deltask_button.pack(padx=1, pady=1)

    #list stuff
    listbox = Listbox(self.main_window, width=100, height = 100, font=('Fixed',20) )
    listbox.pack()


    #print testlist


    Tkinter.mainloop()

def NewTask(self):
    self.newtask_window = Tkinter.Tk()


    self.newtask_window.minsize(250,150)
    self.newtask_window.maxsize(250,150)

    #text
    self.task_label = Tkinter.Label(self.newtask_window, text='Enter Task.')
    self.task_label.pack()

    #entry
    self.task_entry = Tkinter.Entry(self.newtask_window, width=30)
    self.task_entry.pack()


    #button
    self.task_button = Tkinter.Button(self.newtask_window, text='Ok', command = self.NewTaskCount, width = 20)
    self.task_button.pack()


    Tkinter.mainloop()

def NewTaskCount(self):
    listbox = Listbox(self.main_window, width=100, height = 100, font=('Fixed',20) )
    listbox.pack()
    self._count += 1
    self.newtask_window.destroy()

def DelTask(self):
    tkMessageBox.showinfo('Title', 'Task Deleted')

 program = ToDoList()

【问题讨论】:

    标签: python list function listbox tkinter


    【解决方案1】:

    您错误地使用了 Tkinter。您应该始终只创建一个 Tk 类的实例,并在程序的生命周期内只调用一次 mainloop。如果你想要多个窗口,在创建Tk 的实例后,任何其他窗口都需要是Toplevel 的实例。

    【讨论】:

    • 您将如何替换 Tk()?
    • @user1840752:就像我在回答中所说,如果您需要多个窗口,请创建Toplevel 的实例。
    • 如何将列表传递到 TopLevel?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多