Home > Software design >  First numbers stuck together and horizontal in the label (tkinter)
First numbers stuck together and horizontal in the label (tkinter)

Time:11-11

im makin a machine for prime number,But I want the output of the numbers to be stuck together and horizontal, for example:

>>2,3,5,7,11,13,......

but machine do this:

enter image description here

It is neither horizontal nor joined together my code:

from tkinter import *
def prime():
    a = int(spin.get())
    for number in range(a   1):
        if number > 1:
            for i in range(2,number):
                if (number % i) == 0:
                    break
            else:
                Label(app,text=number,).pack()
app = Tk()
app.maxsize(300,300)
app.minsize(300,300)
spin = Entry(app,font=20)
spin.pack()
Button(app,text="prime numbers",command=prime).pack()
app.mainloop()

CodePudding user response:

You can do this by storing the numbers in a list (result) and then joining them at the end.

def prime():
    a = int(spin.get())
    result = []
    for number in range(a   1):
        if number > 1:
            for i in range(2,number):
                if (number % i) == 0:
                    break
            else:
                result.append(str(number))
    Label(app, text = ",".join(result)).pack()

The numbers have to be stored as strings so join will work.

Edit

To wrap every 10 numbers, add the following in place of Label(app, ...) after the for loop:

for i in range(0, len(result), 10):
        Label(app, text = ",".join(result[i:i 10])).pack()
  • Related