如何让 ws.append 在函数内部工作
How do I make ws.append work inside a function
ws.append
函数在 def add()
下时不会在 excel 中写入任何内容。但是,如果我将它放在 def add()
之外,它就会起作用。我什么都试过了,但还是不行,我也不知道为什么。
代码如下:
import sys
import tkinter as tk
from tkinter import *
import datetime
from openpyxl import Workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
# file creation
wb = Workbook()
# Window
root = tk.Tk()
root.title('Aranjamente liste')
# Grid
canvas = tk.Canvas(root, width = 600, height= 300)
canvas.grid(columnspan = 3, rowspan = 20)
# Instrucions
instruction = tk.Label(root, text = "Scrieti CNP + NUME si PRENUME / CLUB ", font = "Raleway")
instruction.grid(columnspan = 3, column = 0, row = 0)
# CheckBox
## Variabile
var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
var4 = IntVar()
var5 = IntVar()
var6 = IntVar()
var7 = IntVar()
var8 = IntVar()
var9 = IntVar()
var10 = IntVar()
var11 = IntVar()
var12 = IntVar()
var13 = IntVar()
# Definesc CheckBox
kihon_c = Checkbutton( root, text = "Kihon Dousa", variable = var1, onvalue = 1, offvalue = 0, command = lambda:comanda())
kodachi_c = Checkbutton( root, text = "Kodachi", variable = var2, onvalue = 1, offvalue = 0, command = lambda:comanda() )
tatekodachi_c = Checkbutton( root, text = "Tate Kodachi", variable = var3, onvalue = 1, offvalue = 0, command = lambda:comanda())
nito_c = Checkbutton( root, text = "Nito", variable = var4, onvalue = 1, offvalue = 0, command = lambda:comanda())
chokenFree_c = Checkbutton( root, text = "Choken Free", variable = var5, onvalue = 1, offvalue = 0, command = lambda:comanda())
chokenMorote_c = Checkbutton( root, text = "Choken Morote", variable = var6, onvalue = 1, offvalue = 0, command = lambda:comanda())
tanto_c = Checkbutton( root, text = "Tanto", variable = var7, onvalue = 1, offvalue = 0, command = lambda:comanda())
tateChoken_c = Checkbutton( root, text = "Tate Choken", variable = var8, onvalue = 1, offvalue = 0, command = lambda:comanda())
yari_c = Checkbutton( root, text = "Yari", variable = var9, onvalue = 1, offvalue = 0, command = lambda:comanda())
bo_c = Checkbutton( root, text = "Bo", variable = var10, onvalue = 1, offvalue = 0, command = lambda:comanda())
echipeLupta_c = Checkbutton( root, text = "Echipe Lupta", variable = var11, onvalue = 1, offvalue = 0, command = lambda:comanda())
echipeKhion_c = Checkbutton( root, text = "Echipe Kihon Dousa", variable = var12, onvalue = 1, offvalue = 0, command = lambda:comanda())
aviz_medical_c = Checkbutton(root, text="Aviz Medical", variable = var13, onvalue = 1, offvalue = 0, command = lambda:comanda())
# Le aranjez
kihon_c.grid( columnspan = 1, column = 0, row = 3, sticky=W)
kodachi_c.grid( columnspan = 1, column = 0, row = 4, sticky=W)
tatekodachi_c.grid(columnspan = 1, column = 1, row = 3, sticky=W)
chokenMorote_c.grid(columnspan = 1, column = 1, row = 4, sticky=W)
chokenFree_c.grid(columnspan = 1, column = 2, row = 3, sticky=W)
nito_c.grid(columnspan = 1, column = 2, row = 4, sticky=W)
tanto_c.grid( columnspan = 1, column = 0, row = 5, sticky=W)
tateChoken_c.grid(columnspan = 1, column = 0, row = 6, sticky=W)
yari_c.grid( columnspan = 1, column = 1, row = 5, sticky=W)
bo_c.grid( columnspan = 1, column = 1, row = 6, sticky=W)
echipeLupta_c.grid( columnspan = 1, column = 2, row = 5, sticky=W)
echipeKhion_c.grid( columnspan = 1, column = 2, row = 6, sticky=W)
aviz_medical_c.grid(columnspan = 1, column = 1, row = 7)
def comanda():
print(var1.get())
print(var2.get())
# ADD Button
add_button = tk.Button(root,command = lambda:add(), text = "Adauga", font = "Raleway")
add_button.grid(columnspan=3, column= 0, row = 15 )
# Quit Button
quit_button = tk.Button(root, command = lambda: sys.exit(), text = "Iesire", font = "Raleway")
quit_button.grid(columnspan = 3, column = 0, row = 20)
#### AICI BAGI TOATE FUNCTIILE ####
def add():
ws1 = wb.active
ws1.title = "Baieti"
ws2 = wb.create_sheet("Fete")
nume = text.get(1.14, "2.0-1c")
cnp = text.get(1.0, 1.13)
club = text.get(2.0,'end-1c')
c_cnp = cnp
# Check if the checkboxes are checked
proba1 = "x"
proba2 = "x"
proba3 = "x"
proba4 = "x"
proba5 = "x"
proba6 = "x"
proba7 = "x"
proba8 = "x"
proba9 = "x"
proba10 = "x"
proba11 = "x"
proba12 = "x"
aviz = "Nu are aviz medical"
if var1.get() == 1:
proba1 = "Kihon Dousa"
print(proba1)
if var2.get() == 1:
proba2 = "Kodachi"
print(proba2)
if var3.get() == 1:
proba3 = "Tate Kodachi"
print(proba3)
if var4.get() == 1:
proba4 = "Nito"
print(proba4)
if var5.get() == 1:
proba5 = "Choken Free"
print(proba5)
if var6.get() == 1:
proba6 = "Choken Morote"
print(proba6)
if var7.get() == 1:
proba7 = "Tanto"
print(proba7)
if var8.get() == 1:
proba8 = "Tate Choken"
print(proba8)
if var9.get() == 1:
proba9 = "Yari"
print(proba9)
if var10.get() == 1:
proba10 = "Bo"
print(proba10)
if var11.get() == 1:
proba11 = "Echipe Lupta"
print(proba11)
if var12.get() == 1:
proba12 = "Echipe Kihon Dousa"
print(proba12)
if var13.get() == 1:
aviz = "Are aviz medical"
print(aviz)
varsta = int(c_cnp)/10000000000%100
x = 1999 + varsta
ani = 2022 - x
an_nastere = int(c_cnp)/10000000000%100 + 2000
luna_nastere = int(c_cnp)/100000000%100
zi_nastere = int(c_cnp)/1000000%100
# Data de nastere comparata cu data de azi
data_azi = datetime.date.today()
current_year = datetime.date.today().year
data_sportiv = datetime.date(current_year, int(luna_nastere), int(zi_nastere))
# Compara data de azi cu data de nastere a sportivului
if data_sportiv > data_azi:
ani = ani - 1
if cnp.startswith('5') is True:
ws1.append([cnp])
#elif cnp.startswith('6') == True:
# ff.write(cnp + "\t" + nume + "\t" + str(int(ani)) + " ani" + "\t" + str(int(zi_nastere)) + "." + str(int(luna_nastere)) + "." + str(int(an_nastere)) + "\t" + str(proba1) + "\t" + str(proba2) + "\t" + str(proba3) + "\t" + str(proba4) + "\t" + str(proba5) + "\t" + str(proba6)
# + "\t" + str(proba7) + "\t" + str(proba8) + "\t" + str(proba9) + "\t" + str(proba10) + "\t" + str(proba11) + "\t" + str(proba12) + "\n")
# Se sterge dupa adaugare
text.delete(1.0, END)
kihon_c.deselect()
kodachi_c.deselect()
nito_c.deselect()
chokenFree_c.deselect()
chokenMorote_c.deselect()
tanto_c.deselect()
tatekodachi_c.deselect()
tateChoken_c.deselect()
bo_c.deselect()
yari_c.deselect()
echipeKhion_c.deselect()
echipeLupta_c.deselect()
# Text Box
text = Text(root, width = 40, height = 3, )
text.insert(INSERT, "")
text.grid(columnspan=3, column=0, row=1)
# Stilizare, arata mai bine
canvas = tk.Canvas(root, width = 600, height= 250)
canvas.grid(columnspan = 3)
## Mainloop of the window
wb.save("test.xlsx")
root.mainloop()
没有看到你的代码的其余部分并且不知道“它不工作”是什么意思(错误?做其他事情?)我们不能肯定,但看起来可能是因为 ws1
不是 add()
函数命名空间的一部分。
尝试添加 ws1
作为 add()
的参数,然后在您实际调用 add()
时包含它。像这样:
#some things, other functions maybe, code blah blah
def add(ws):
cnp = text.get(1.0,1.13)
if cnp.startswith('5') is True: # you should also use "is True" instead of "== True", fyi
ws.append([cnp])
#more code, blah blah
add(ws1)
#blah blah blah
我发现 wb.save("test.xlsx")
行必须放在 def add(ws1)
中,否则 excel 文件不会保存任何内容。
ws.append
函数在 def add()
下时不会在 excel 中写入任何内容。但是,如果我将它放在 def add()
之外,它就会起作用。我什么都试过了,但还是不行,我也不知道为什么。
代码如下:
import sys
import tkinter as tk
from tkinter import *
import datetime
from openpyxl import Workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
# file creation
wb = Workbook()
# Window
root = tk.Tk()
root.title('Aranjamente liste')
# Grid
canvas = tk.Canvas(root, width = 600, height= 300)
canvas.grid(columnspan = 3, rowspan = 20)
# Instrucions
instruction = tk.Label(root, text = "Scrieti CNP + NUME si PRENUME / CLUB ", font = "Raleway")
instruction.grid(columnspan = 3, column = 0, row = 0)
# CheckBox
## Variabile
var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
var4 = IntVar()
var5 = IntVar()
var6 = IntVar()
var7 = IntVar()
var8 = IntVar()
var9 = IntVar()
var10 = IntVar()
var11 = IntVar()
var12 = IntVar()
var13 = IntVar()
# Definesc CheckBox
kihon_c = Checkbutton( root, text = "Kihon Dousa", variable = var1, onvalue = 1, offvalue = 0, command = lambda:comanda())
kodachi_c = Checkbutton( root, text = "Kodachi", variable = var2, onvalue = 1, offvalue = 0, command = lambda:comanda() )
tatekodachi_c = Checkbutton( root, text = "Tate Kodachi", variable = var3, onvalue = 1, offvalue = 0, command = lambda:comanda())
nito_c = Checkbutton( root, text = "Nito", variable = var4, onvalue = 1, offvalue = 0, command = lambda:comanda())
chokenFree_c = Checkbutton( root, text = "Choken Free", variable = var5, onvalue = 1, offvalue = 0, command = lambda:comanda())
chokenMorote_c = Checkbutton( root, text = "Choken Morote", variable = var6, onvalue = 1, offvalue = 0, command = lambda:comanda())
tanto_c = Checkbutton( root, text = "Tanto", variable = var7, onvalue = 1, offvalue = 0, command = lambda:comanda())
tateChoken_c = Checkbutton( root, text = "Tate Choken", variable = var8, onvalue = 1, offvalue = 0, command = lambda:comanda())
yari_c = Checkbutton( root, text = "Yari", variable = var9, onvalue = 1, offvalue = 0, command = lambda:comanda())
bo_c = Checkbutton( root, text = "Bo", variable = var10, onvalue = 1, offvalue = 0, command = lambda:comanda())
echipeLupta_c = Checkbutton( root, text = "Echipe Lupta", variable = var11, onvalue = 1, offvalue = 0, command = lambda:comanda())
echipeKhion_c = Checkbutton( root, text = "Echipe Kihon Dousa", variable = var12, onvalue = 1, offvalue = 0, command = lambda:comanda())
aviz_medical_c = Checkbutton(root, text="Aviz Medical", variable = var13, onvalue = 1, offvalue = 0, command = lambda:comanda())
# Le aranjez
kihon_c.grid( columnspan = 1, column = 0, row = 3, sticky=W)
kodachi_c.grid( columnspan = 1, column = 0, row = 4, sticky=W)
tatekodachi_c.grid(columnspan = 1, column = 1, row = 3, sticky=W)
chokenMorote_c.grid(columnspan = 1, column = 1, row = 4, sticky=W)
chokenFree_c.grid(columnspan = 1, column = 2, row = 3, sticky=W)
nito_c.grid(columnspan = 1, column = 2, row = 4, sticky=W)
tanto_c.grid( columnspan = 1, column = 0, row = 5, sticky=W)
tateChoken_c.grid(columnspan = 1, column = 0, row = 6, sticky=W)
yari_c.grid( columnspan = 1, column = 1, row = 5, sticky=W)
bo_c.grid( columnspan = 1, column = 1, row = 6, sticky=W)
echipeLupta_c.grid( columnspan = 1, column = 2, row = 5, sticky=W)
echipeKhion_c.grid( columnspan = 1, column = 2, row = 6, sticky=W)
aviz_medical_c.grid(columnspan = 1, column = 1, row = 7)
def comanda():
print(var1.get())
print(var2.get())
# ADD Button
add_button = tk.Button(root,command = lambda:add(), text = "Adauga", font = "Raleway")
add_button.grid(columnspan=3, column= 0, row = 15 )
# Quit Button
quit_button = tk.Button(root, command = lambda: sys.exit(), text = "Iesire", font = "Raleway")
quit_button.grid(columnspan = 3, column = 0, row = 20)
#### AICI BAGI TOATE FUNCTIILE ####
def add():
ws1 = wb.active
ws1.title = "Baieti"
ws2 = wb.create_sheet("Fete")
nume = text.get(1.14, "2.0-1c")
cnp = text.get(1.0, 1.13)
club = text.get(2.0,'end-1c')
c_cnp = cnp
# Check if the checkboxes are checked
proba1 = "x"
proba2 = "x"
proba3 = "x"
proba4 = "x"
proba5 = "x"
proba6 = "x"
proba7 = "x"
proba8 = "x"
proba9 = "x"
proba10 = "x"
proba11 = "x"
proba12 = "x"
aviz = "Nu are aviz medical"
if var1.get() == 1:
proba1 = "Kihon Dousa"
print(proba1)
if var2.get() == 1:
proba2 = "Kodachi"
print(proba2)
if var3.get() == 1:
proba3 = "Tate Kodachi"
print(proba3)
if var4.get() == 1:
proba4 = "Nito"
print(proba4)
if var5.get() == 1:
proba5 = "Choken Free"
print(proba5)
if var6.get() == 1:
proba6 = "Choken Morote"
print(proba6)
if var7.get() == 1:
proba7 = "Tanto"
print(proba7)
if var8.get() == 1:
proba8 = "Tate Choken"
print(proba8)
if var9.get() == 1:
proba9 = "Yari"
print(proba9)
if var10.get() == 1:
proba10 = "Bo"
print(proba10)
if var11.get() == 1:
proba11 = "Echipe Lupta"
print(proba11)
if var12.get() == 1:
proba12 = "Echipe Kihon Dousa"
print(proba12)
if var13.get() == 1:
aviz = "Are aviz medical"
print(aviz)
varsta = int(c_cnp)/10000000000%100
x = 1999 + varsta
ani = 2022 - x
an_nastere = int(c_cnp)/10000000000%100 + 2000
luna_nastere = int(c_cnp)/100000000%100
zi_nastere = int(c_cnp)/1000000%100
# Data de nastere comparata cu data de azi
data_azi = datetime.date.today()
current_year = datetime.date.today().year
data_sportiv = datetime.date(current_year, int(luna_nastere), int(zi_nastere))
# Compara data de azi cu data de nastere a sportivului
if data_sportiv > data_azi:
ani = ani - 1
if cnp.startswith('5') is True:
ws1.append([cnp])
#elif cnp.startswith('6') == True:
# ff.write(cnp + "\t" + nume + "\t" + str(int(ani)) + " ani" + "\t" + str(int(zi_nastere)) + "." + str(int(luna_nastere)) + "." + str(int(an_nastere)) + "\t" + str(proba1) + "\t" + str(proba2) + "\t" + str(proba3) + "\t" + str(proba4) + "\t" + str(proba5) + "\t" + str(proba6)
# + "\t" + str(proba7) + "\t" + str(proba8) + "\t" + str(proba9) + "\t" + str(proba10) + "\t" + str(proba11) + "\t" + str(proba12) + "\n")
# Se sterge dupa adaugare
text.delete(1.0, END)
kihon_c.deselect()
kodachi_c.deselect()
nito_c.deselect()
chokenFree_c.deselect()
chokenMorote_c.deselect()
tanto_c.deselect()
tatekodachi_c.deselect()
tateChoken_c.deselect()
bo_c.deselect()
yari_c.deselect()
echipeKhion_c.deselect()
echipeLupta_c.deselect()
# Text Box
text = Text(root, width = 40, height = 3, )
text.insert(INSERT, "")
text.grid(columnspan=3, column=0, row=1)
# Stilizare, arata mai bine
canvas = tk.Canvas(root, width = 600, height= 250)
canvas.grid(columnspan = 3)
## Mainloop of the window
wb.save("test.xlsx")
root.mainloop()
没有看到你的代码的其余部分并且不知道“它不工作”是什么意思(错误?做其他事情?)我们不能肯定,但看起来可能是因为 ws1
不是 add()
函数命名空间的一部分。
尝试添加 ws1
作为 add()
的参数,然后在您实际调用 add()
时包含它。像这样:
#some things, other functions maybe, code blah blah
def add(ws):
cnp = text.get(1.0,1.13)
if cnp.startswith('5') is True: # you should also use "is True" instead of "== True", fyi
ws.append([cnp])
#more code, blah blah
add(ws1)
#blah blah blah
我发现 wb.save("test.xlsx")
行必须放在 def add(ws1)
中,否则 excel 文件不会保存任何内容。