【问题标题】:Error in trying to open dialog box from a widget button in python尝试从 python 中的小部件按钮打开对话框时出错
【发布时间】:2023-03-13 14:18:01
【问题描述】:

我是 python 新手。我正在尝试打开一个对话框以从一个小部件中获取一个值,该小部件已经完成了其他员工的列表。

但是遇到错误,不知道该怎么办。

这是我的代码:

import Tkinter,Tkconstants,tkFileDialog
from Tkinter import *
import csv
import numpy
import math
import numpy.random as nrnd
import matplotlib.pyplot as plt
import shutil
import tkMessageBox
global filesavepath
class App:
    def __init__(self,master):
        self.mymaster=master
        frame=Frame(master)
        frame.pack()
        self.importbutton=Button(frame,text='Import Data',command=self.importdata)
        self.importbutton.pack()
        self.executebutton=Button(frame,text='Execute',command=self.popup)
        self.executebutton.pack()
        self.distribution_rep=Button(frame,text='Repeat Purchase Score Distribution',command=self.distrepbutton)
        self.distribution_rep.pack()
        self.distribution_churn=Button(frame,text='Churn Probability Distribution',command=self.distchurnbutton)
       self.distribution_churn.pack()
       self.exitbutton=Button(frame,text='Exit',command=self.exitapp)
       self.exitbutton.pack()
       self.file_opt=options={}
       options['defaultextension']=''
       options['filetypes']=[('allfiles','.*'),('textfiles','.txt')]
       options['initialdir']='C:\\'
       options['initialfile']='myfile.txt'
       options['parent']=root
       options['title']='Thisisatitle'
    def importdata(self):
        filename=tkFileDialog.askopenfilename(**self.file_opt)
        filesavepath="C:/input_full.csv"
        shutil.copy2(filename,filesavepath)
        if filename:
            return open(filename,'r')

    def popup(self):
        top = self.top = Tkinter.Toplevel(self)
        myLabel = Tkinter.Label(top, text='Enter your username below')
        myLabel.pack()

        self.myEntryBox = Tkinter.Entry(top)
        self.myEntryBox.pack()

        mySubmitButton = Tkinter.Button(top, text='Done', command=self.execbutton)
        mySubmitButton.pack()
    def execbutton(self):
        if self.myEntryBox.get() != "":
            self.timevalue = self.myEntryBox.get()
            self.top.destroy()
        execfile("Repeat Purchase Algo in python v6")
        tkMessageBox.showinfo("Job Done", "Probability Computation completed")       
    def send(self):
        global timevalue
        timevalue=self.myEntryBox.get()
        self.top.destroy()
    def distrepbutton(self):
        plt.hist(prob,bins=10,normed=TRUE)
        plt.xlabel('Probability')
        plt.title('Histogram of Repeat Purchase Probability')
        plt.show()
    def distchurnbutton(self):
        plt.hist(churn_prob,bins=10,normed=TRUE)
        plt.ylabel('Probability')
        plt.title('Histogram of Churn Probability')
        plt.show()
    def exitapp(self):
        self.mymaster.destroy()

root=Tk()
root.title('Repeat Puchase Widget')
app=App(root)
root.mainloop()

您可能很清楚,我正在使用 Import 按钮导入数据集,通过名为 Execute 的按钮在另一个代码中执行一些分析,然后显示一些图表。

我想要的是在单击“执行”按钮时打开一个弹出式窗口,该窗口将输入一个值。但我收到以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:/Python27/widget_repeat_purchase_v4", line 42, in popup
    top = self.top = Tkinter.Toplevel(self)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2017, in __init__
    BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1965, in __init__
    BaseWidget._setup(self, master, cnf)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1943, in _setup
    self.tk = master.tk
AttributeError: App instance has no attribute 'tk'

我不知道该怎么办。请帮忙。

【问题讨论】:

  • 如果你想知道为什么有这么多导入,请注意它们是我在这里运行的第二个 python 脚本

标签: python popup widget tkinter dialog


【解决方案1】:

当您创建顶级小部件时,您将 self 作为第一个参数传递。 Tkinter 要求这是一个父小部件。但是,在您的代码中 self 并不代表小部件。

在您的具体情况下,您希望传入self.mymaster 而不是self

top = self.top = Tkinter.Toplevel(self.mymaster)

【讨论】:

  • 感谢 Bryan,它成功了...我没有意识到错误...真的很感谢
【解决方案2】:

使用Tkinter.Toplevel() 代替Tkinter.Toplevel(self)

【讨论】:

    猜你喜欢
    • 2011-12-31
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多