首页 > 解决方案 > python3中的tkSimpleDialog用法

问题描述

我正在开发我的程序以使其符合 python3 标准,但它严重依赖 tkinter,而且我遇到了一些问题。

我已经更改了所有导入名称,因为其中许多已重命名(哪些 tkinter 模块在 Python 3 中被重命名?),但我该如何处理以下问题?

class PasswordDialog(tkSimpleDialog.Dialog):

我尝试将其更改为

class PasswordDialog(tkinter.simpledialog.dialog):

但它一直告诉我AttributeError: 'module' object has no attribute 'dialog'

(非)工作代码:

#!/usr/bin/python3

from tkinter import simpledialog

class PasswordDialog(simpledialog.Dialog):                 
    def body(self, master):
        Label(master, text="Password for file "+currentfn).grid(row=0, sticky=W)
        self.e1 = Entry(master,show='*')
        self.e1.grid(row=0, column=1)
        self.attributes("-topmost", True) 
        return self.e1 
    def apply(self):
        p = self.e1.get()
        self.result = p

结果:

AttributeError: 'module' object has no attribute 'Dialog'

标签: python-3.xtkinter

解决方案


在 Windows 10 机器上使用 Python 3.74。我按原样采用了您的代码,并刚刚定义了 currentfn,它对我有用。这是我的整个代码清单


##  ------------------------------------------------------------------------
##  Generic Template Program
##  ------------------------------------------------------------------------


##  ------------------------------------------------------------------------
##  Imports
##  ------------------------------------------------------------------------
from tkinter import *
from tkinter import ttk

from tkinter import simpledialog


##  ------------------------------------------------------------------------
##  ToDo List
##  ------------------------------------------------------------------------


##  ------------------------------------------------------------------------
##  Constants
##  ------------------------------------------------------------------------
prog_id = {'progname':'Sample Template',
    'title':'Simple gui template',
    'version':'1.0',
    'date':'05 March 2020',
    'rev_date':'',
    'author':'Peter Hedlund',
    'description':'Create simple template file.'}


##  ------------------------------------------------------------------------
##  Variables
##  ------------------------------------------------------------------------
_debug_ = 0
currentfn='test'

##  ------------------------------------------------------------------------
##  Functions
##  ------------------------------------------------------------------------

class PasswordDialog(simpledialog.Dialog):                 
    def body(self, master):
        Label(master, text="Password for file "+currentfn).grid(row=0, sticky=W)
        self.e1 = Entry(master,show='*')
        self.e1.grid(row=0, column=1)
        self.attributes("-topmost", True) 
        return self.e1 
    def apply(self):
        p = self.e1.get()
        self.result = p


   

##  ------------------------------------------------------------------------
##  Controls
##  ------------------------------------------------------------------------


def quit_prog():
    root.destroy()


def test_btn():
    PasswordDialog(root, "Password Test")

##  ------------------------------------------------------------------------
##
##  Main GUI Starts Here
##
##  ------------------------------------------------------------------------
root = Tk()
#  prevent window resizing.
root.resizable(0, 0)
root.title(prog_id['title'] + "  Ver " + prog_id['version'])
mainframe = ttk.Frame(root, padding='3', height=590, width=850)
mainframe.grid(column=1, row=2, sticky=(N, W, E, S))
mainframe.grid_propagate(0)

#  Style Section
s1 = ttk.Style()
s1.configure('red.TButton', background='Red')

#  String variables
fbus_cw = StringVar()

#  Integer variables
reportType = IntVar()

#  Label Widget
lab0 = Label(mainframe, text='', width=15)
lab0.grid(column=0, row=5)

#  Button Widgets
savelog = ttk.Button(mainframe, text='Test', command=test_btn)
savelog.grid(column=0, row=8)
quitProg = ttk.Button(mainframe, text='Quit', style='red.TButton',
                      command=quit_prog)
quitProg.grid(column=0, row=10)

for child in mainframe.winfo_children():
    child.grid_configure(padx=5, pady=5)

root.mainloop()

推荐阅读