I want to print out multiple lines of text from Python Tkinter module.

Asked 2 years ago, Updated 2 years ago, 89 views

import random as rd

def lottery_number():
    num = int(number.get())
    for i in range(num):
        lotto_list = list(range(1,46))   #from 1 to 45
        rd.shuffle(lotto_list) #shuffle
        lotto_list1 = lotto_list[:6]  
        lotto_list1.sort() #sorting 
        answer = '{}  Auto'.format(i), lotto_list1
    result.config(text=answer)

from tkinter import *

window = Tk()
label = Label(window, text = 'press lottery game number.')
label.pack()

number =Entry(window, width = 30)
number.pack()

btn = Button(window, text = 'Click', fg = 'blue', command = lottery_number)
btn.pack()

result = Label(window, text = 'Result')
result.pack()

window.mainloop()

The result value is printed in one line like this, so I want to modify it.

I printed it out because I thought there was a problem with the for statement, but the for statement was printed in several lines normally

                          {1} Auto 3, 5, 9, 30 ,33
                          {2} Auto 4, 6, 9, 23 ,44
                          {3} Auto 1, 7, 9, 20 ,26

I want the above result.

Actually, I would like to arrange the order in alphabet as below and print it out on tkinter window. I don't know how to sort the alphabet. I ask for your help me.

                         {A} Auto 3, 5, 9, 30 ,33
                         {B} Auto 4, 6, 9, 23 ,44
                         {C} Auto 1, 7, 9, 20 ,26

python3 tkinter for

2022-09-20 19:02

1 Answers

Please refer to the code below.

import random as rd

def lottery_number():
    num = int(number.get())
    answer = ''
    for i in range(num):
        lotto_list = list(range(1,46)) #from 1 to 45
        rd.shuffle(lotto_list) #shuffle
        lotto_list1 = lotto_list[:6]
        lotto_list1.sort() #sorting 

        answer += '{' + '%s' % chr(ord('A') + i) + '} Auto '
        for j in range(len(lotto_list1)):
            answer += '%2s' % lotto_list1[j]
            if j != = len(lotto_list1)-1:
                answer += ', '
        if i != num-1:
            answer += '\n'

    result.config(text=answer)

from tkinter import *

window = Tk()
label = Label(window, text = 'press lottery game number.')
label.pack()

number = Entry(window, width = 30)
number.pack()

btn = Button(window, text = 'Click', fg = 'blue', command = lottery_number)
btn.pack()

result = Label(window, text = 'Result', font = 'TkFixedFont')
result.pack()

window.mainloop()


2022-09-20 19:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.