2017-06-03 56 views
1

我在python中使用openpyxl和tkinter製作了一個excel GUI程序。這裏是我的代碼:openpyxl GUI程序出現多個錯誤

import openpyxl 
from tkinter import * 
from tkinter import Tk 
from tkinter.filedialog import askopenfilename 
from openpyxl.utils import get_column_letter, column_index_from_string 


root = Tk() 
root.configure(background='light green') 
root.geometry("500x500") 
root.wm_title("BananaCell") 

v1 = StringVar() 
v2 = StringVar() 
v3 = StringVar() 
v4 = StringVar() 


e1 = Entry(root, textvariable=v1) 
e1.insert(10,'ddd') 
e1.delete(0, END) 
e1.pack() 
e1.place(x=70, y=150) 


e = Entry(root) 
e.insert(10,"Sheet 1 Name: ") 
e.delete(0, END) 
e.pack() 
e.place(x=300, y=150) 

v1 = e.get() 
print(v1) 

def get_1(): 
    print(v1) 

bf = Button(root, text="Enter", width=6, height=0, command=get_1) 
bf.pack() 
bf.place(x=15, y=147) 

def askForFileName1(): 
    f1 = askopenfilename(title="Select Workbook 1") 
    wb = openpyxl.load_workbook(str(f1)) 


def askForFileName2(): 
    f2 = askopenfilename(title="Select Workbook 2") 
    wb1 = openpyxl.load_workbook(str(f2)) 

b = Button(root, text="Workbook 1", width=12, height=2, command=askForFileName1) 
b.pack() 
b.place(x=100, y=100) 

b2 = Button(root, text="Workbook 2", width=12, height=2, 
command=askForFileName2) 
b2.pack() 
b2.place(x=300, y=100) 


mainloop() 

col1 = input('Column letter from Sheet 1 to compare from: ') 
col2 = input('Column letter from Sheet 2 to compare from: ') 

for (col, col_1) in zip(ws.iter_cols(min_col = column_index_from_string(col1), max_col=column_index_from_string(col1)), 
ws1.iter_cols(min_col = column_index_from_string(col2), 
max_col=column_index_from_string(col2))): 
    for (cell, cell_1) in zip(col, col_1): 
     if cell.value != cell_1.value and cell.row == cell_1.row: 
      print('Row ' + str(cell.row) + ' ' + str(cell.value) + ' is not equal to ' + str(cell_1.value) + ' ' + 'Row ' + str(cell_1.row)) 

exit_if = input('Press x to exit when you\'re ready: ') 

我有兩個問題:一個是我需要的變量f1f2是全球性的莫名其妙和B是當我按在節目中Enter按鈕,它打印的空白區域。誰能解決這些問題嗎?任何幫助是巨大的

+0

只是想知道:你是否試圖在Tkinter中重新創建Excel? – DeepSpace

+1

另外,你是什麼意思是「我需要變量f1和f2是全球性*某種程度*」?什麼阻止你使它們成爲全局的,即'全球f1,f2'? – DeepSpace

+0

@DeepSpace沒有我使用openpyxl製作的Excel比較程序,但該工藝的圖形(如按鈕和文本框)是Tkinter的 – Bill

回答

-1

的問題答:分配之外的功能的任何值即

f1,f2=0,0 

,並使其全球內部功能

def ask_for_filename(): 
    global f1 #or global f2 whatever you want 

,每次你使用這些變量之外的功能,他們給你理想的結果不要忘記讓他們內部功能全局如果你希望他們修改爲全球:

global f1 or f2 
+0

在Python中使用'global'幾乎總是要避免的,因爲幾乎總是有一個更好的解決方案,比如實例變量。 –

+0

亞我得到了。但他(要求的人)想要利用全球f1和f2,所以我建議他這樣。謝謝 –

+0

那又如何?僅僅因爲他認爲他需要全局變量,並不意味着他這麼做。 –