在 python 的函数中创建一个自动递增循环
Creating an auto incrementing loop within a function in python
我正在尝试设置一个循环来跟踪多个 ordernum 并为每个新订单分配一个 nw ordernum 以增加价值。
完整代码如下:
*这是使用 Klaus D. 的建议的固定代码
import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from itertools import count
class ImprintPlusApp:
def __init__(self, master):
self.counter = count(1)
master.title("Imprint Plus Manager")
self.frame_header = ttk.Frame(master)
self.frame_header.pack()
ttk.Label(self.frame_header, text = "Bienvenidos a Imprint Plus Manager")
self.frame_crearorden = ttk.Frame(master)
self.frame_crearorden.pack()
ttk.Label(self.frame_crearorden, text = "Nombre").grid(row = 0, column = 0, padx = 5)
ttk.Label(self.frame_crearorden, text = "Email").grid(row = 2, column = 0, padx = 5)
ttk.Label(self.frame_crearorden, text = "Numero Cel/Tel").grid(row = 4, column = 0, padx = 5)
ttk.Label(self.frame_crearorden, text = "Información Adicional").grid(row = 6, column = 0, padx = 5)
self.entry_name = ttk.Entry(self.frame_crearorden, width = 24)
self.entry_email = ttk.Entry(self.frame_crearorden, width = 24)
self.entry_numtc = ttk.Entry(self.frame_crearorden, width = 24)
self.entry_addinf = Text(self.frame_crearorden, width = 50, height = 10)
self.entry_name.grid(row = 0, column = 1, padx = 5)
self.entry_email.grid(row = 2, column = 1, padx = 5)
self.entry_numtc.grid(row = 4, column = 1, padx = 5)
self.entry_addinf.grid(row = 7, column = 0, columnspan = 2, padx = 5)
ttk.Button(self.frame_crearorden, text = "Submit", command = self.submit).grid(row = 8, column = 1,columnspan = 1, padx = 5)
ttk.Button(self.frame_crearorden, text = "Clear", command = self.clear).grid(row = 8, columnspan = 1, padx = 5)
def submit(self):
result = next(self.counter)
orderResult = str(result)
print ("Nombre: {}".format(self.entry_name.get()))
print ("Email: {}".format(self.entry_email.get()))
print ("Num Cel/Tel: {}".format(self.entry_numtc.get()))
print ("Información Adicional: {}".format(self.entry_addinf.get(1.0, "end")))
self.clear()
messagebox.showinfo(title = "Orden #"+ orderResult, message = "Orden Guardada")
def clear(self):
self.entry_name.delete(0, "end")
self.entry_email.delete(0, "end")
self.entry_numtc.delete(0, "end")
self.entry_addinf.delete(1.0, "end")
def main():
root = Tk()
app = ImprintPlusApp(root)
root.mainloop()
if __name__ == '__main__':
main()
您可以使用 itertools.count()
。它创建了一个生成器,只有 returns 一个接一个,当你调用它的 next()
方法时:
from itertools import count
counter = count(1)
for row in rows:
row_id = counter.next()
在本例中,从给定的参数(1
)开始,您将在每次遍历行时获得 row_id 的另一个值:1
、2
, 3
, …
生成器也可直接用于 for
循环:
for number in count(1):
print(number)
这将快速打印一个数字列表。
你 class 最好在 __init__()
中创建计数器并在需要时调用 next()
:
def __init__(self, master):
self.counter = count(1)
…(more code)…
def submit(self):
my_id = self.counter.next()
…(more code)…
我正在尝试设置一个循环来跟踪多个 ordernum 并为每个新订单分配一个 nw ordernum 以增加价值。
完整代码如下:
*这是使用 Klaus D. 的建议的固定代码
import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from itertools import count
class ImprintPlusApp:
def __init__(self, master):
self.counter = count(1)
master.title("Imprint Plus Manager")
self.frame_header = ttk.Frame(master)
self.frame_header.pack()
ttk.Label(self.frame_header, text = "Bienvenidos a Imprint Plus Manager")
self.frame_crearorden = ttk.Frame(master)
self.frame_crearorden.pack()
ttk.Label(self.frame_crearorden, text = "Nombre").grid(row = 0, column = 0, padx = 5)
ttk.Label(self.frame_crearorden, text = "Email").grid(row = 2, column = 0, padx = 5)
ttk.Label(self.frame_crearorden, text = "Numero Cel/Tel").grid(row = 4, column = 0, padx = 5)
ttk.Label(self.frame_crearorden, text = "Información Adicional").grid(row = 6, column = 0, padx = 5)
self.entry_name = ttk.Entry(self.frame_crearorden, width = 24)
self.entry_email = ttk.Entry(self.frame_crearorden, width = 24)
self.entry_numtc = ttk.Entry(self.frame_crearorden, width = 24)
self.entry_addinf = Text(self.frame_crearorden, width = 50, height = 10)
self.entry_name.grid(row = 0, column = 1, padx = 5)
self.entry_email.grid(row = 2, column = 1, padx = 5)
self.entry_numtc.grid(row = 4, column = 1, padx = 5)
self.entry_addinf.grid(row = 7, column = 0, columnspan = 2, padx = 5)
ttk.Button(self.frame_crearorden, text = "Submit", command = self.submit).grid(row = 8, column = 1,columnspan = 1, padx = 5)
ttk.Button(self.frame_crearorden, text = "Clear", command = self.clear).grid(row = 8, columnspan = 1, padx = 5)
def submit(self):
result = next(self.counter)
orderResult = str(result)
print ("Nombre: {}".format(self.entry_name.get()))
print ("Email: {}".format(self.entry_email.get()))
print ("Num Cel/Tel: {}".format(self.entry_numtc.get()))
print ("Información Adicional: {}".format(self.entry_addinf.get(1.0, "end")))
self.clear()
messagebox.showinfo(title = "Orden #"+ orderResult, message = "Orden Guardada")
def clear(self):
self.entry_name.delete(0, "end")
self.entry_email.delete(0, "end")
self.entry_numtc.delete(0, "end")
self.entry_addinf.delete(1.0, "end")
def main():
root = Tk()
app = ImprintPlusApp(root)
root.mainloop()
if __name__ == '__main__':
main()
您可以使用 itertools.count()
。它创建了一个生成器,只有 returns 一个接一个,当你调用它的 next()
方法时:
from itertools import count
counter = count(1)
for row in rows:
row_id = counter.next()
在本例中,从给定的参数(1
)开始,您将在每次遍历行时获得 row_id 的另一个值:1
、2
, 3
, …
生成器也可直接用于 for
循环:
for number in count(1):
print(number)
这将快速打印一个数字列表。
你 class 最好在 __init__()
中创建计数器并在需要时调用 next()
:
def __init__(self, master):
self.counter = count(1)
…(more code)…
def submit(self):
my_id = self.counter.next()
…(more code)…