我正在使用openpyxl和tkinter在python中创建一个excel 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: ')
我有两个问题:A是我需要变量f1
和f2
以某种方式是全局的,B是当我按下程序中的Enter
按钮时,它打印出一个空格处。谁能解决这些问题?任何帮助都很棒
答案 0 :(得分:-1)
:在函数之外分配任何值,即
f1,f2=0,0
并使它们在函数内部全局
def ask_for_filename():
global f1 #or global f2 whatever you want
每次使用这些变量外部函数时,它们都会为您提供所需的结果,如果您希望将它们修改为全局函数,请不要忘记将它们设置为函数全局:
global f1 or f2