Reputation: 3
So I'm coding a tkinter programm which sorts the random numbers with different sorting algorithms. You have to insert the range of numbers to sort, then you press enter and it shows you the unsorted numbers on the right, after you choose a sorting algorithm and press sort and it prints out the sorted numbers on the right. But my problem is, that after I execute the programm, the tinker window is just blank. I'm still a beginner.
import random
try:
from Tkinter import *
except ImportError:
from tkinter import *
MAX = 999
MIN = 0
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
master.minsize(width=600, height=400)
self.grid()
def create_widgets(self):
Label(self,
text="Dieses Programm sortiert die angegebene Anzahl von Zahlen nach einem Sortierverfahren."
).grid(row=0, column=0, columnspan=2, sticky=W)
Label(self,
text="Wie viele Zahlen wollen Sie sortieren?"
).grid(row=2, column=0, columnspan=2, sticky=W)
Label(self,
text="Hier sind die sortierten Zahlen"
).grid(row=0, column=4, columnspan=2, sticky=W)
Label(self,
text="Welches Sortierverfahren wollen sie nutzen?"
).grid(row=4, column=0, columnspan=2, sticky=W)
self.anzahl_ent=Entry(self)
self.anzahl_ent.grid(row=3, column=1, sticky=W)
Button(self,
text="Eingabe",
command=self.get_anzahl
).grid(row=3, column=2, sticky=W)
self.v=IntVar()
Radiobutton(self,
text="Bubblesort",
variable=v,
value=1
).grid(row=5,column=1, sticky=W)
Radiobutton(self,
text="Quicksort",
variable=v,
value=2
).grid(row=6, column=1, sticky=W)
Button(self,
text="SORTIEREN",
command=self.Zahlsortieren
).grid(row=7, column=1, sticky=W)
self.sortierteZahlen_txt = Text(self, width=50, heigh=200)
self.sortierteZahlen_txt.grid(row=2, column=4, sticky=W)
def get_anzahl(self):
self.anzahl=int(self.anzahl_ent.get())
self.ranZahlen()
def ranZahlen(self):
self.liste = [random.randrange(MIN, MAX) for _ in xrange(anzahl)]
self.zahlen_ausgeben(self.liste)
def Zahlsortieren(self):
if v==2:
def partition(arr, low, high):
i = (low - 1)
pivot = arr[high]
for j in range(low, high):
if arr[j] <= pivot:
i = i + 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return (i + 1)
def quickSort(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quickSort(arr, low, pi - 1)
quickSort(arr, pi + 1, high)
n = len(liste)
quickSort(liste, 0, n - 1)
for i in range(n):
self.zahlenliste1=print("%d" % liste[i])
self.zahlen_ausgeben(self.zahlenliste1)
return
elif v==1:
def bubbleSort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
bubbleSort(liste)
for i in range(len(liste)):
self.zahlenliste2=print("%d" % liste[i])
self.zahlen_ausgeben(self.zahlenliste2)
return
def zahlen_ausgeben(self, message):
self.sortierteZahlen_txt.delete(0.0, END)
self.sortierteZahlen_txt.insert(0.0, message)
def main():
root=Tk()
root.title("Sortierverfahren")
app = Application(root)
root.mainloop()
main()
Upvotes: 0
Views: 47
Reputation: 331
You need to call create_widgets
in order to display anything. Change your main method to be:
def main():
root=Tk()
root.title("Sortierverfahren")
app = Application(root)
app.create_widgets()
root.mainloop()
Upvotes: 1
Reputation: 5560
Your calls to quickSort
or bubbleSort
are blocking. Take a look at this question to see how to dispatch background jobs that don't block the UI:
Python+Tkinter, how to run in background independant of tk frontend?
Upvotes: 0