2014-10-20 11 views
-1

我正在嘗試在其中輸入公司名稱,聯繫人姓名,電話號碼和評論的Python腳本(在我的Linux框中使用Python 2.7.6)。當我輸入信息時,我點擊一個按鈕,然後打開一個文件進行閱讀並遍歷它,以查看在輸入數據之前,公司名稱或電話號碼是否已存在於文件中。如果其中之一或兩者之前已經記錄過,我希望它在tkMessageBox中顯示這個事實,例如顯示電話號碼被記錄了3次。 除了數字是完全錯誤的以外,它都可以工作。如果實際數爲3,它會顯示各種數字也許426或105或任何其他number.below的是代碼的一部分,因爲我有它目前:在tkMessageBox中顯示數字

#!/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項不同的語法建議。 有人可以幫助我嗎?

+1

什麼是e1.get()和e2.get()?你的腳本有多個問題,這是完整的程序嗎?這應該不會運行... – Parker 2014-10-20 21:27:35

+0

e1和e2是輸入公司名稱和電話號碼的輸入框 – Buteman 2014-10-20 21:34:04

+0

它們在哪裏定義?很難幫助只有一個代碼 – Parker 2014-10-20 21:36:02

回答

1

你的輸出是186而不是7,因爲這些線路:

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

您不應該以這種方式使用str.findfind如果未找到該字符串,則返回-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 

這是正確的結果。

+0

感謝您解決了我的問題。 – Buteman 2014-10-21 05:15:02

+0

@Parker感謝您的努力。正如你們將不會見到我是Tkinter的真正初學者。 – Buteman 2014-10-21 05:18:08

+0

@smoothgrips感謝您的努力 – Buteman 2014-10-21 05:18:27

2

我從未使用過的Tkinter之前,但快速谷歌搜索顯示我們IntVar是一個類,當你在Python中實例化類,則需要使用括號,就像這樣:

countname = IntVar() 
counttel = IntVar() 

當你要值設置爲對象,使用設置功能,就像這樣:

countname.set(0) 

當你想檢索對象的值,可以使用得到功能,像這樣:

countname.get() 

此外,聲明countname是一個IntVar(),但在第二天線,設置該對象到正規的整型,所以它不再是一個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

+0

這樣看起來有意義但是此語法的countname的輸出爲186。我在tkMessage Box行後面添加了一個打印語句,並從終端運行代碼,並且打印也給了countname 186,即使文本文件現在只顯示12。 – Buteman 2014-10-20 21:52:39

+0

我編輯了原始代碼,以便您可以看到整個腳本。 – Buteman 2014-10-20 21:58:53

+0

您是否分別刪除了'countname = IntVar()'和'counttel = IntVar()'行之後的'countname = 0'和'counttel = 0'行? – smoothgrips 2014-10-20 22:02:00