在tkMessageBox中显示数字

时间:2014-10-20 21:07:45

标签: python tkinter

我正在尝试构建一个python脚本(在我的Linux机器上使用Python 2.7.6),我在其中输入公司名称,联系人姓名,电话号码和评论。当我输入信息时,我单击一个按钮,然后打开一个文件进行读取并迭代它以查看公司名称或电话号码是否已经在文件中,然后将我输入的数据附加到文件中。如果之前已经记录了其中一个或两个,我希望它在tkMessageBox中显示这个事实,例如,显示电话号码被记录3次。 除了数字完全错误外,一切正常。如果真实计数为3,它将显示各种数字,可能是426或105或任何其他数字。下面是我目前所拥有的代码的一部分:

#!/usr/bin/env python
from Tkinter import *
import time
import tkMessageBox
from ScrolledText import *
titles = 'Company', 'Contact', 'Tel', 'Comments'
fields = 'e1', 'e2', 'e3', 'e4'

def show_entry_titles():
    countname = IntVar    
    counttel = IntVar    
    filename = "callers.txt"
    myfile = open(filename, 'r') 
    company= e1.get()
    tel=e2.get()
    for myline in myfile:
        q = myline
        if q.find(company):
            countname += 1
        if q.find(tel):
            counttel += 1
    myfile.close()

    if countname + counttel > 0:
        msg = "Company " + str(countname)
        tkMessageBox.showinfo(countname)

    localtime = time.asctime( time.localtime(time.time()) )
    localtime = "Logged on: " + localtime + "\n"
    company = "Company " + e1.get() + "\n"
    contact = "Contact " + e2.get() + "\n"
    tel = "Tel Num " + e3.get() + "\n"
    comm = "Comments: " + e4.get('1.0', END+'-1c') + "\n"
    filename = "callers.txt"
    myfile = open(filename, 'a')
    myfile.write(localtime)
    myfile.write(company)
    myfile.write(contact)
    myfile.write(tel)
    myfile.write(comm)
    myfile.write("-----------------------------------\n")
    myfile.close()
    e1.delete(0,END)
    e2.delete(0,END)
    e3.delete(0,END)
    e4.delete('0.0', 'end')
master = Tk()

for k in range(4):
    Label(master, text=titles[k]).grid(row=k)

e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)
e4 = ScrolledText(master, width=40, height=10)
e1.insert(20,"")
e2.insert(20,"")
e3.insert(20,"")
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
e4.grid(row=3, column=1)

Button(master, text='Quit', command=master.quit).grid(row=k+1, column=0, sticky=W, pady=4)
Button(master, text='Show', command=show_entry_titles).grid(row=k+1, column=1, sticky=W, pady=4)

mainloop( )

作为发布代码的示例,我知道我输入的特定电话号码已经在文件中7次,但消息是:

closed file 'callers.txt', mode 'r' at 0x7f870074c0c0

将tkMessageBox行作为:

tkMessageBox.showinfo(msg)

消息显示为:

Company 174

我花了几个小时搜索解决方案,但找不到任何正确显示字符串和int的引用,我必须尝试至少5种不同的语法建议。 请有人帮我这个吗?

2 个答案:

答案 0 :(得分:2)

我之前从未使用过Tkinter,但是快速Google搜索向我们展示了IntVar是一个类,当你在Python中实例化类时,你需要使用括号,如下所示:

countname = IntVar()
counttel = IntVar()

如果要为对象设置值,可以使用 set 函数,如下所示:

countname.set(0)

如果要从对象中检索值,可以使用 get 函数,如下所示:

countname.get()

此外,您将countname声明为IntVar(),但在下一行中,您将该对象设置为常规int,因此它不再是IntVar()。

以下代码可能会解决您的问题:

countname = IntVar(0)
counttel = IntVar(0)
...
if q.find(company):
    countname.set(countname.get() + 1)
if q.find(tel):
    counttel.set(counttel.get() + 1)
...
msg = "Company " + str(countname.get())
tkMessageBox.showinfo(countname.get())

来源:

Tkinter.IntVar

Tkinterbook: The Variable Classes

答案 1 :(得分:1)

您的输出是186而不是7,因为这些行:

    if q.find(company):
        countname += 1
    if q.find(tel):
        counttel += 1

您不应该以这种方式使用str.find。如果找不到字符串,则find返回-1,否则返回子字符串开头的索引。对于除零以外的任何整数,bool(someInt)评估为True,因此对于文件中的每一行,countnamecounttel都会递增,但以公司开头的行除外姓名或电话号码。如果你想检查字符串是否在另一个字符串中,我建议使用in运算符。

    if company in q:
        countname += 1
    if tel in q:
        counttel += 1

作为一个更简单的例子,这实际上就是你正在做的事情:

lines = [
    "Hello, how are you today?",
    "Today I went to Starbucks and bought a coffee.",
    "It was delicious."
]

countname = 0
company = "Starbucks"
for line in lines:
    if line.find(company):
        countname += 1

print "We found the company name 'Starbucks' this many times: " + str(countname)

结果:

We found the company name 'Starbucks' this many times: 3

这是不正确的结果。 “星巴克”只出现一次,而不是三次。让我们再试一次使用in

lines = [
    "Hello, how are you today?",
    "Today I went to Starbucks and bought a coffee.",
    "It was delicious."
]

countname = 0
company = "Starbucks"
for line in lines:
    if company in line:
        countname += 1

print "We found the company name 'Starbucks' this many times: " + str(countname)

结果:

We found the company name 'Starbucks' this many times: 1

这是正确的结果。